@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_code = 'XYZ25';
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
, .
, .
UPDATE
, .
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';
if ( $coupon->get_code() == $fake_code ) return $discount;
$max_discount = 250;
$coupon_code = 'XYZ25';
if ( $coupon->get_code() == $coupon_code ) {
$_coupon = new WC_Coupon( );
$_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;
}