修复 WooCommerce 中购物车百分比的最大优惠券折扣
Fix maximum coupon Discount On Cart percentage in WooCommerce
我在 woocommerce 中有一个优惠券代码 (XYZ25),其中包含 25% 的折扣,最大折扣为 Rs.250。
如果用户使用优惠券代码 XYZ25 获得 25% 的折扣,我如何限制他们获得不超过 250 卢比的折扣。
Since Woocommerce 3.2 or 3.3, this code doesn't work anymore
您可以根据 **[=12= 的固定购物车折扣设置额外的优惠券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+.
上进行测试
正如@LoicTheAztec 指出的那样。它可以是固定折扣,也可以是百分比折扣。
代码如下:
add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
$max_discount = 250; // coupon limit
$coupon_code = 'XYZ25'; // coupon to check.
if ( ( $coupon->get_code() == $coupon_code ) && ! is_null( $cart_item ) && WC()->cart->subtotal_ex_tax ) {
$cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
if ( wc_prices_include_tax() ) {
$discount_percent = ( wc_get_price_including_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal;
} else {
$discount_percent = ( wc_get_price_excluding_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal_ex_tax;
}
$_discount = ( $max_discount * $discount_percent ) / $cart_item_qty;
$discount = min( $_discount, $discount );
}
return $discount;
}
这也将使用 "Fixed cart discount" 逻辑计算折扣;并使用 $max_discount
作为优惠券金额进行计算。然后两者中较小的那个用完。
简单来说,就拿这个例子来说吧min( A, B )
。 A
为最大折扣,B
为百分比折扣计算结果。
分钟( 250, 100 ) = 100
分钟 ( 250, 150 ) = 150
分钟 ( 250, 250 ) = 250
分钟 ( 250, 300 ) = 250
分钟 ( 250, 600 ) = 250
因此总能获得所需的最大折扣。
I have written more code relating to this here.
更新
另一种方法,但结果相同。
add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
$fake_code = 'fake_code_abcdefghigklmnopqrstuvwxyz'; // used to ignore this filter
if ( $coupon->get_code() == $fake_code ) return $discount;
$max_discount = 250; // coupon limit
$coupon_code = 'XYZ25'; // coupon to check.
if ( $coupon->get_code() == $coupon_code ) {
$_coupon = new WC_Coupon( ); // lets create a fake coupon to test our $max_discount.
$_coupon->set_props( array(
'discount_type' => 'fix_cart',
'amount' => $max_discount,
'code' => $fake_code
) );
$_discount = $_coupon->get_discount_amount( $discounting_amount, $cart_item, $single );
$discount = min( $_discount, $discount );
}
return $discount;
}
我在 woocommerce 中有一个优惠券代码 (XYZ25),其中包含 25% 的折扣,最大折扣为 Rs.250。
如果用户使用优惠券代码 XYZ25 获得 25% 的折扣,我如何限制他们获得不超过 250 卢比的折扣。
Since Woocommerce 3.2 or 3.3, this code doesn't work anymore
您可以根据 **[=12= 的固定购物车折扣设置额外的优惠券
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+.
上进行测试正如@LoicTheAztec 指出的那样。它可以是固定折扣,也可以是百分比折扣。
代码如下:
add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
$max_discount = 250; // coupon limit
$coupon_code = 'XYZ25'; // coupon to check.
if ( ( $coupon->get_code() == $coupon_code ) && ! is_null( $cart_item ) && WC()->cart->subtotal_ex_tax ) {
$cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
if ( wc_prices_include_tax() ) {
$discount_percent = ( wc_get_price_including_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal;
} else {
$discount_percent = ( wc_get_price_excluding_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal_ex_tax;
}
$_discount = ( $max_discount * $discount_percent ) / $cart_item_qty;
$discount = min( $_discount, $discount );
}
return $discount;
}
这也将使用 "Fixed cart discount" 逻辑计算折扣;并使用 $max_discount
作为优惠券金额进行计算。然后两者中较小的那个用完。
简单来说,就拿这个例子来说吧min( A, B )
。 A
为最大折扣,B
为百分比折扣计算结果。
分钟( 250, 100 ) = 100
分钟 ( 250, 150 ) = 150
分钟 ( 250, 250 ) = 250
分钟 ( 250, 300 ) = 250
分钟 ( 250, 600 ) = 250
因此总能获得所需的最大折扣。
I have written more code relating to this here.
更新 另一种方法,但结果相同。
add_filter( 'woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 20, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
$fake_code = 'fake_code_abcdefghigklmnopqrstuvwxyz'; // used to ignore this filter
if ( $coupon->get_code() == $fake_code ) return $discount;
$max_discount = 250; // coupon limit
$coupon_code = 'XYZ25'; // coupon to check.
if ( $coupon->get_code() == $coupon_code ) {
$_coupon = new WC_Coupon( ); // lets create a fake coupon to test our $max_discount.
$_coupon->set_props( array(
'discount_type' => 'fix_cart',
'amount' => $max_discount,
'code' => $fake_code
) );
$_discount = $_coupon->get_discount_amount( $discounting_amount, $cart_item, $single );
$discount = min( $_discount, $discount );
}
return $discount;
}