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

标签 php wordpress woocommerce custom-taxonomy coupon

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

  • attribute_pa_style => 漩涡状
  • attribute_pa_style => 圆圈

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

感谢任何帮助。

最佳答案

这可以通过以下方式使用 woocommerce_coupon_is_valid 过滤器 Hook 来完成:

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 文件中。经过测试并有效。

关于php - 从 Woocommerce 中的优惠券使用中排除具有 2 个特定属性术语的变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54297527/

相关文章:

php - Imagick 宝丽来效果没有透明度

javascript - 无法正确使用 Handlebars.js 在我的 'template' 中显示变量

javascript - 单击按钮时显示数据库中的 WordPress 数据库自定义表行列值

javascript - 如何在 facebook Pixel javascript 代码中使用 WordPress 自定义插件选项的值

WooCommerce:如何在 Ajax 刷新审核订单和付款方式部分后运行 JS 代码?

php - 使用 PHP AES CCM 模式解密的意外结果

php/mysql 排序问题和表单清理?

php - 基于字符串动态创建 PHP 对象

php - 打折时更改 Woocommerce 产品名称

php - 在 WooCommerce 中获取可变产品的所有变体属性和价格