问题
I have a coupon code (XYZ25) in woocommerce which include 25% off and maximum discount is Rs.250.
How can i restrict user's to not get more than Rs.250 Discount if they apply coupon code XYZ25 for 25% discount.
回答1:
Since Woocommerce 3.2 or 3.3, this code doesn't work anymore
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
.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 replaceXYZ25
coupon byFIX250
displaying at the same time an explicative notice…
Here is that code:
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' );
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This working code is tested on WooCommerce versions 2.6.x and 3.0+.
回答2:
As @LoicTheAztec pointed out. It's either a fixed or a percentage discount.
Here's the code:
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;
}
What this will do is also calculate the discount using "Fixed cart discount" logic; and using the $max_discount
as the coupon amount to compute. Then whichever is the smaller among the two gets used up.
To simply put it, let's take this example min( A, B )
. A
is the max discount, and B
is the result of percent discount calculation.
min( 250, 100 ) = 100
min( 250, 150 ) = 150
min( 250, 250 ) = 250
min( 250, 300 ) = 250
min( 250, 600 ) = 250
Thus resulting to always getting the desired max discount.
I have written more code relating to this here.
UPDATE another way of doing it but same result.
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;
}
来源:https://stackoverflow.com/questions/43887663/fix-maximum-coupon-discount-on-cart-percentage-in-woocommerce