我在 woocommerce 中有优惠券代码 (XYZ25),其中包含 25% 的折扣,最高折扣为 Rs.250。
如果用户使用优惠券代码 XYZ25 获得 25% 的折扣,我如何限制用户获得不超过 250 卢比的折扣。
最佳答案
Since Woocommerce 3.2 or 3.3, this code doesn't work anymore
您可以根据 **
RS.250
的固定购物车折扣设置额外的优惠券FIX250
代码 (不含税)并且最低消费(4 x 250) = RS.1000
。然后在下面脚本的帮助下,如果客户应用您的
XYZ25
优惠券代码并且如果购物车总计达到 Rs.1000,它将用FIX250
替换XYZ25
优惠券,同时显示一个解释性通知......
这是代码:
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+ 上进行测试。
关于php - 修复 WooCommerce 中购物车百分比的最大优惠券折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43887663/