【发布时间】:2017-10-08 19:38:09
【问题描述】:
我在 woocommerce 中有一个优惠券代码 (XYZ25),其中包括 25% 的折扣,最高折扣为 250 卢比。
如果用户使用优惠券代码 XYZ25 获得 25% 的折扣,我如何限制他们获得的折扣不超过 250 卢比。
【问题讨论】:
标签: php woocommerce cart coupon discount
我在 woocommerce 中有一个优惠券代码 (XYZ25),其中包括 25% 的折扣,最高折扣为 250 卢比。
如果用户使用优惠券代码 XYZ25 获得 25% 的折扣,我如何限制他们获得的折扣不超过 250 卢比。
【问题讨论】:
标签: php woocommerce cart coupon discount
从 Woocommerce 3.2 或 3.3 开始,此代码不再起作用
您可以设置额外的优惠券FIX250 代码,基于固定购物车折扣 **RS.250 (不含税) 并且最低消费为(4 x 250) = RS.1000。
然后在下面的脚本的帮助下,如果客户应用您的 XYZ25 优惠券代码并且如果购物车总数达到 Rs.1000,它将替换 @ 987654325@优惠券由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+ 上测试。
【讨论】:
正如@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;
}
这还将使用“固定购物车折扣”逻辑计算折扣;并使用$max_discount 作为要计算的优惠券金额。然后两者中较小的那个被用完。
简单来说,我们以min( A, B )为例。 A 是最大折扣,B 是百分比折扣计算的结果。
分钟( 250, 100 ) = 100
分钟( 250, 150 ) = 150
分钟( 250, 250 ) = 250
分钟( 250, 300 ) = 250
min(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;
}
【讨论】: