从 Woocommerce 中的优惠券使用中排除具有 2 个特定属性术语的变体

2023-12-07

如果客户的购物车中有任何具有以下属性条款的特定产品变体,我需要防止使用优惠券:

  • attribute_pa_style => swirly
  • attribute_pa_style => circle

我查看了适用于限制特定产品和特定类别的 Woocommerce 脚本,但无法弄清楚属性和所有优惠券。

任何帮助表示赞赏。


这可以使用以下方法完成woocommerce_coupon_is_valid过滤钩子这样:

add_filter( 'woocommerce_coupon_is_valid', 'check_if_coupons_are_valid', 10, 3 );
function check_if_coupons_are_valid( $is_valid, $coupon, $discount ){
    // YOUR ATTRIBUTE SETTINGS BELOW:
    $taxonomy   = 'pa_style';
    $term_slugs = array('swirly', 'circle');

    // Loop through cart items and check for backordered items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        foreach( $cart_item['variation'] as $attribute => $term_slug ) {
            if( $attribute === 'attribute_'.$taxonomy && in_array( $term_slug, $term_slugs ) ) {
                $is_valid = false; // attribute found, coupons are not valid
                break; // Stop and exit from the loop
            }
        }
    }
    return $is_valid;
}

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Woocommerce 中的优惠券使用中排除具有 2 个特定属性术语的变体 的相关文章

随机推荐