自定义价格,如果产品ID =

时间:2019-03-26 05:37:58

标签: php wordpress hook-woocommerce

我有一个自定义表格,用于覆盖WooCommerce价格。该表称为wp_get_pricing。当前,以下代码(以及其他多次尝试)在第一个IF语句处停止,并使所有产品具有相同的价格,即使以下IF语句中没有产品ID。

function return_custom_price($product, $price) {
global $savedzip;   

$products_array = wc_get_product( id );

$column_ids='';
if ($products_array = '846') {
$column_ids = 'five_resident';  }
elseif ($products_array = '1477') {
$column_ids = 'six_resident';}
elseif ($products_array = '1603') {
$column_ids = 'thirty_commercial';}
elseif ($products_array = '1602') {
$column_ids = 'twenty_commercial';}
elseif ($products_array = '1600') {
$column_ids = 'fifteen_commercial';}
elseif ($products_array = '1322') {
$column_ids = 'thirty_resident';}
elseif ($products_array = '1316') {
$column_ids = 'twenty_resident';}
elseif ($products_array = '1314') {
$column_ids = 'fifteen_resident';}
elseif ($products_array = '1313') {
$column_ids = 'ten_resident';}
else  {
    $column_ids = "residential";} 

$price = $wpdb->get_var("SELECT $column_ids FROM wp_get_pricing WHERE zip='$savedzip'");
return $price;

 }
  add_filter('woocommerce_get_price', 'return_custom_price', 10, 2); 

如果我将$ products_array更改为= $ product-> id(),则网站的前端将返回空白页。任何帮助表示赞赏。

更新 我改变了

 $products_array = wc_get_product( id ) 

 $FindID = $wpdb->get_results("SELECT DISTINCT ID FROM wp_posts WHERE post_type = 'product' AND post_status='publish'", ARRAY_A);

当我执行var_dump时,这给了我以下内容

 array(20) { [0]=> array(1) { ["ID"]=> string(2) "81" } [1]=> array(1) { ["ID"]=> string(3) "577" } [2]=> array(1) { ["ID"]=> string(3) "846" } [3]=> array(1) { ["ID"]=> string(4) "1306" } [4]=> array(1) { ["ID"]=> string(4) "1311" } [5]=> array(1) { ["ID"]=> string(4) "1313" } [6]=> array(1) { ["ID"]=> string(4) "1314" } [7]=> array(1) { ["ID"]=> string(4) "1316" } [8]=> array(1) { ["ID"]=> string(4) "1322" } [9]=> array(1) { ["ID"]=> string(4) "1324" } [10]=> array(1) { ["ID"]=> string(4) "1477" } [11]=> array(1) { ["ID"]=> string(4) "1592" } [12]=> array(1) { ["ID"]=> string(4) "1593" } [13]=> array(1) { ["ID"]=> string(4) "1598" } [14]=> array(1) { ["ID"]=> string(4) "1600" } [15]=> array(1) { ["ID"]=> string(4) "1602" } [16]=> array(1) { ["ID"]=> string(4) "1603" } [17]=> array(1) { ["ID"]=> string(4) "1605" } [18]=> array(1) { ["ID"]=> string(4) "1665" } [19]=> array(1) { ["ID"]=> string(4) "1670" } }

将$ products_array =更新为

$products_array = $findID->ID;

IF $ products_array =仅返回第一个值,并使所有产品的价格与ID 846的产品相同。

如果$ products_array ==返回ELSE

1 个答案:

答案 0 :(得分:0)

当然必须这样做,因为=是一个赋值运算符。如果将846成功分配给$products_array,则将执行条件代码。您应该使用==进行比较。