Php 允许将某些电子商务优惠券设置为";“个人使用”;一起应用
我们为所有优惠券打上了“个人使用”的标记,以阻止多张优惠券被使用 有一个例外,我们需要这些“个人使用”优惠券可用于其他特定优惠券 例如: 共3张优惠券: 欢迎10-个人使用-10%折扣 欢迎20人-个人使用-八折 欢迎30-30%的折扣 Welcome 10和Welcome 20将无法通过验证,因为目前的情况是,Welcome 10或Welcome 20都可以与Welcome 30一起使用 所以Welcome 30将基本上覆盖个人使用的验证Php 允许将某些电子商务优惠券设置为";“个人使用”;一起应用,php,wordpress,woocommerce,cart,coupon,Php,Wordpress,Woocommerce,Cart,Coupon,我们为所有优惠券打上了“个人使用”的标记,以阻止多张优惠券被使用 有一个例外,我们需要这些“个人使用”优惠券可用于其他特定优惠券 例如: 共3张优惠券: 欢迎10-个人使用-10%折扣 欢迎20人-个人使用-八折 欢迎30-30%的折扣 Welcome 10和Welcome 20将无法通过验证,因为目前的情况是,Welcome 10或Welcome 20都可以与Welcome 30一起使用 所以Welcome 30将基本上覆盖个人使用的验证 这可能吗?请注意,在WooCommerce代码中,要使
这可能吗?请注意,在WooCommerce代码中,要使用的优惠券代码是优惠券slug(因此没有大写字母和空格) 因此,我用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文件。经过测试,效果良好。这很好,但我们唯一的问题是:如果我们先在购物车中应用welcome-10(一次性使用),然后再应用welcome-30,那么它将非常有效!如果我们先使用welcome-30,然后再使用welcome-10(一次性使用),它将从购物车中删除welcome-30。这非常有效,谢谢!如果我有一系列优惠券与另一系列一次性优惠券兼容,这将如何工作?@HarryRobinson这不能在评论中得到回答……有一系列优惠券,这将是复杂的,取决于你真正的要求。您唯一可以更改的是
array('welcome10','welcome20')
,使用包含许多优惠券的较大数组。