从Woocommerce的优惠券使用中排除带有2个特定属性项的变体 [英] Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce

查看:84
本文介绍了从Woocommerce的优惠券使用中排除带有2个特定属性项的变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to prevent coupons being used if customer have any specific product variations in their cart with following attribute terms:


  • attribute_pa_style =>涡流状的

  • attribute_pa_style =>圈子

  • attribute_pa_style => swirly
  • attribute_pa_style => circle

我浏览了适用于限制特定产品和特定类别的Woocommerce脚本,但是

I've looked through the Woocommerce scripts that apply to restricting specific products and specific categories, but can't figure it out with regard to attributes and all coupons.

不胜感激。

推荐答案

可以通过以下方式使用 woocommerce_coupon_is_valid 过滤器挂钩来完成此操作:

This can be done using woocommerce_coupon_is_valid filter hook this way:

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文件)。经过测试,可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于从Woocommerce的优惠券使用中排除带有2个特定属性项的变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆