Php 在商业中有条件地改变多个特定产品的价格
我在这里使用的代码是:将特定产品的价格提高10美元,但在某个类别的产品页面上以及购物车是否包含该类别的任何内容除外 但是现在我有不止一种产品需要涨价。我试着改变第7行:Php 在商业中有条件地改变多个特定产品的价格,php,wordpress,woocommerce,price,woocommerce-bookings,Php,Wordpress,Woocommerce,Price,Woocommerce Bookings,我在这里使用的代码是:将特定产品的价格提高10美元,但在某个类别的产品页面上以及购物车是否包含该类别的任何内容除外 但是现在我有不止一种产品需要涨价。我试着改变第7行: if( $product->get_id() != 87 ) return $price_html; 差不多 if( $product->get_id() != 87 || $product->get_id() != 2799 ) return $price_html; 以产品87或2799为目标,但它只是
if( $product->get_id() != 87 ) return $price_html;
差不多
if( $product->get_id() != 87 || $product->get_id() != 2799 ) return $price_html;
以产品87或2799为目标,但它只是破坏了代码,甚至产品87也不再显示为多出10美元。我尝试过| |的变体,但我所做的一切都不管用
非常感谢您的帮助:)您的if条件没有意义,因为它总是返回true。尝试用和替换或:
对于多个产品ID,而不是使用以下内容:
add_filter( 'some_hook', 'some_function' );
function some_function( $price_html, $product ){
if( $product->get_id() != 87 || $product->get_id() != 2799 ) return $price_html;
// The function code
return $price_html; // at the end
}
您将使用以下内容:
add_filter( 'some_hook', 'some_function' );
function some_function( $price_html, $product ){
if( in_array( $product->get_id(), array( 87, 2799 ) ) ){
// The function code
}
return $price_html; // at the end
}
而且它可以用于多种产品的阵列,上面的代码片段看起来不错。也许是别的什么东西在影响它。你能分享整个代码块以便我们调试吗?谢谢!将第7行更改为
if(!in_array($product->get_id(),array(872799)))返回$price_html;//退出
效果很好!谢谢@IvnH,我的编码能力不在PHP范围内,所以理解逻辑对我来说是一段相当长的学习过程。我现在明白了区别:)
add_filter( 'some_hook', 'some_function' );
function some_function( $price_html, $product ){
if( in_array( $product->get_id(), array( 87, 2799 ) ) ){
// The function code
}
return $price_html; // at the end
}