php - 从 Woocommerce 中的优惠券使用中排除具有 2 个特定属性术语的变体
问题描述
如果客户的购物车中有任何具有以下属性术语的特定产品变体,我需要防止使用优惠券:
attribute_pa_style => swirly
attribute_pa_style => circle
我查看了适用于限制特定产品和特定类别的 Woocommerce 脚本,但无法弄清楚属性和所有优惠券。
任何帮助表示赞赏。
解决方案
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;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
推荐阅读
- sql - 是否可以在不使用 SQL 连接的情况下对不同年份进行每周比较?
- mongodb - $group 与 mongodb 中的嵌套数组
- javascript - Vue.js 将对象传递给其他组件
- selenium - Selenium/Testng:如果发生异常,则跳过或失败其余的 @Test
- angular - 具有 DomSanitizer 依赖项的 Angular 6 单元测试组件
- android - 使用 BroadcastReceiver 获取 rssi 值
- docker - 命令不在 docker-compose.yml 中执行
- c# - C#:复制文件夹中的多个文件并保留文件夹结构
- apache-spark - 将密钥分配给 pyspark rdd 中的所有值
- html - 从顶部对齐 li 项目