我们的所有优惠券都有“单独使用”,以阻止多张优惠券被使用。
有一个例外,我们需要这些“个人使用”的优惠券可以用于另一个特定的优惠券。
例如:
共有3张优惠券:欢迎10 -个人使用- 10%优惠欢迎20 -个人使用- 20%优惠欢迎30 - 30%的折扣
欢迎10和欢迎20将失败的验证,因为它目前这样做,但欢迎10或欢迎20可以使用欢迎30。
因此,欢迎30实际上将覆盖individual_use的验证。
这个是可能的吗?
发布于 2021-02-08 22:21:07
注意,在WooCommerce代码中,要使用的优惠券代码是优惠券段塞(因此没有大写,也没有空格)。
因此,我已经测试了下面的代码与3优惠券代码Welcome10
,Welcome20
和Welcome30
,所有3套“个人使用”选项限制(所以优惠券代码段是welcome10
,welcome20
和welcome30
)。
允许welcome30
优惠券代码与welcome10
或welcome20
一起使用的代码
add_filter( 'woocommerce_apply_individual_use_coupon', 'filter_apply_individual_use_coupon', 10, 3 );
function filter_apply_individual_use_coupon( $coupons_to_keep, $the_coupon, $applied_coupons ) {
if ( $the_coupon->get_code() === 'welcome30' ) {
foreach( $applied_coupons as $key => $coupon_code ) {
if( in_array( $coupon_code, array('welcome10', 'welcome20') ) ) {
$coupons_to_keep[$key] = $applied_coupons[$key];
}
}
} elseif ( in_array( $the_coupon->get_code(), array('welcome10', 'welcome20') ) ) {
foreach( $applied_coupons as $key => $coupon_code ) {
if( $coupon_code == 'welcome30' ) {
$coupons_to_keep[$key] = $applied_coupons[$key];
}
}
}
return $coupons_to_keep;
}
add_filter( 'woocommerce_apply_with_individual_use_coupon', 'filter_apply_with_individual_use_coupon', 10, 4 );
function filter_apply_with_individual_use_coupon( $apply, $the_coupon, $applied_coupon, $applied_coupons ) {
if ( $the_coupon->get_code() === 'welcome-30' && in_array( $applied_coupon->get_code(), array('welcome10', 'welcome20') ) ) {
$apply = true;
} elseif ( in_array( $the_coupon->get_code(), array('welcome10', 'welcome20') ) && $applied_coupon->get_code() === 'welcome30' ) {
$apply = true;
}
return $apply;
}
代码位于活动子主题(或活动主题)的functions.php文件中。测试和工作。
https://stackoverflow.com/questions/66109603
复制相似问题