在WooCommerce中修复购物车上最大优惠券折扣百分比 [英] Fix maximum coupon Discount On Cart percentage in WooCommerce
问题描述
我在woocommerce中有一个优惠券代码(XYZ25),其中包括25%的折扣,最大折扣是250卢比.
I have a coupon code (XYZ25) in woocommerce which include 25% off and maximum discount is Rs.250.
如果用户使用优惠券代码XYZ25享受25%的折扣,我如何限制用户的折扣不超过250卢比.
How can i restrict user's to not get more than Rs.250 Discount if they apply coupon code XYZ25 for 25% discount.
推荐答案
自Woocommerce 3.2或3.3以来,此代码不再起作用
Since Woocommerce 3.2 or 3.3, this code doesn't work anymore
-
您可以基于固定的购物车折扣**
RS.250
(不含税)设置其他优惠券FIX250
代码 ,并且最低支出为(4 x 250) = RS.1000
.
You could set an additional coupon
FIX250
code based on a fixed cart discount of **RS.250
(without tax) and with a Minimun spend of(4 x 250) = RS.1000
.
然后在以下脚本的帮助下,如果客户应用您的 XYZ25
优惠券代码,并且购物车总额达到1000卢比,它将替换 XYZ25
优惠券,由 FIX250
同时显示明确的通知…
Then with the help of the script below, if customer apply your XYZ25
coupon code and if the cart total is up to Rs.1000, it will replace XYZ25
coupon by FIX250
displaying at the same time an explicative notice…
这是代码:
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+版本上进行了测试.
This working code is tested on WooCommerce versions 2.6.x and 3.0+.
这篇关于在WooCommerce中修复购物车上最大优惠券折扣百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!