admin管理员组文章数量:1405579
Originally, I only needed to disable coupons from working on one product, so this filter worked well. However, over time, I've added to it a few times. But it looks messy!
Is there a better way to format this?
add_filter( 'woocommerce_coupon_is_valid_for_product', 'quadlayers_exclude_product_from_product_promotions', 9999, 4 );
function quadlayers_exclude_product_from_product_promotions( $valid, $product, $coupon, $values ) {
if ( 49134 == $product->get_id() ) {
$valid = false;
}
if ( 49137 == $product->get_id() ) {
$valid = false;
}
if ( 29873 == $product->get_id() ) {
$valid = false;
}
if ( 43181 == $product->get_id() ) {
$valid = false;
}
if ( 49317 == $product->get_id() ) {
$valid = false;
}
if ( 114863 == $product->get_id() ) {
$valid = false;
}
if ( 117949 == $product->get_id() ) {
$valid = false;
}
if ( 120076 == $product->get_id() ) {
$valid = false;
}
if ( 122660 == $product->get_id() ) {
$valid = false;
}
if ( 129323 == $product->get_id() ) {
$valid = false;
}
return $valid;
}
Originally, I only needed to disable coupons from working on one product, so this filter worked well. However, over time, I've added to it a few times. But it looks messy!
Is there a better way to format this?
add_filter( 'woocommerce_coupon_is_valid_for_product', 'quadlayers_exclude_product_from_product_promotions', 9999, 4 );
function quadlayers_exclude_product_from_product_promotions( $valid, $product, $coupon, $values ) {
if ( 49134 == $product->get_id() ) {
$valid = false;
}
if ( 49137 == $product->get_id() ) {
$valid = false;
}
if ( 29873 == $product->get_id() ) {
$valid = false;
}
if ( 43181 == $product->get_id() ) {
$valid = false;
}
if ( 49317 == $product->get_id() ) {
$valid = false;
}
if ( 114863 == $product->get_id() ) {
$valid = false;
}
if ( 117949 == $product->get_id() ) {
$valid = false;
}
if ( 120076 == $product->get_id() ) {
$valid = false;
}
if ( 122660 == $product->get_id() ) {
$valid = false;
}
if ( 129323 == $product->get_id() ) {
$valid = false;
}
return $valid;
}
Share
Improve this question
edited Mar 23 at 12:18
LoicTheAztec
255k24 gold badges399 silver badges446 bronze badges
asked Mar 23 at 10:23
adem007adem007
1014 bronze badges
1 Answer
Reset to default 2You can set your Products IDs in an array and use PHP conditional function in_array()
like:
add_filter( 'woocommerce_coupon_is_valid_for_product', 'quadlayers_exclude_product_from_product_promotions', 9999, 4 );
function quadlayers_exclude_product_from_product_promotions( $valid, $product, $coupon, $values ) {
$targeted_ids = array(49134, 49137, 29873, 43181, 49317, 114863, 117949, 120076, 122660, 129323);
if ( in_array($product->get_id(), $targeted_ids) ) {
$valid = false;
}
return $valid;
}
It should work.
本文标签: phpExclude multiple product IDs from WooCommerce coupons in an efficient wayStack Overflow
版权声明:本文标题:php - Exclude multiple product IDs from WooCommerce coupons in an efficient way - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744290153a2599073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论