我在 woocommerce 中有一个优惠券代码 (XYZ25),其中包含 25% 的折扣,最大折扣为 250 卢比。
如果用户应用优惠券代码 XYZ 25 获得 25% 的折扣,我如何限制用户获得不超过 250 卢比的折扣。
自 Woocommerce 3.2 或 3.3 起,此代码不再有效
你可以设置额外优惠券FIX250
code基于一个固定购物车折扣**RS.250
(不含税)并与最低消费为(4 x 250) = RS.1000
.
然后,在以下脚本的帮助下,如果客户应用您的XYZ25
优惠券代码,如果购物车总额达到 1000 卢比,它将替换XYZ25
优惠券由FIX250
同时显示一条解释性通知……
这是该代码:
add_action( 'woocommerce_calculate_totals', 'coupon_discount_max_switch', 10, 1);
function coupon_discount_max_switch( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your 2 coupons slugs <=== <=== <=== <=== <=== <=== <=== <=== <===
$coupon_25_percent = 'xyz25';
$coupon_25_fixed = 'fix250';
// Set HERE the limit amount <=== <=== <=== <=== <=== <=== <=== <=== <=== <===
$limit = 250; // Without VAT
$total_discount = $cart_obj->get_cart_discount_total(); // Total cart discount
// When 'xyz25' is set and the total discount is reached
if( $cart_obj->has_discount( $coupon_25_percent ) && $limit_icl_vat <= $total_discount ){
// Remove the 'xyz25' coupon
$cart_obj->remove_coupon( $coupon_25_percent );
// Checking that the fixed dicount is not already set.
if( ! $cart_obj->has_discount( $coupon_25_fixed ) ){
// Add the 'fix250' coupon
$cart_obj->add_discount( $coupon_25_fixed );
// Displaying a custom message
$message = __( "The cart discount limit of Rs.$limit is reached", "woocommerce" );
wc_add_notice( $message, 'notice' );
}
}
}
代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
此工作代码在 WooCommerce 版本 2.6.x 和 3.0+ 上进行了测试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)