WooCommerce set maximum coupon discount

Setting max discount for coupons on WooCommerce is easy if you have this php code in your functions.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
add_action( 'woocommerce_coupon_options_usage_limit', 'woocommerce_coupon_options_usage_limit', 10, 2 );
function woocommerce_coupon_options_usage_limit( $coupon_id, $coupon ){
 
	echo '<div class="options_group">';
	// max discount per coupons
	$max_discount =  get_post_meta( $coupon_id, '_max_discount', true );
	woocommerce_wp_text_input( array(
		'id'                => 'max_discount',
		'label'             => __( 'Usage max discount', 'woocommerce' ),
		'placeholder'       => esc_attr__( 'Unlimited discount', 'woocommerce' ),
		'description'       => __( 'The maximum discount this coupon can give.', 'woocommerce' ),
		'type'              => 'number',
		'desc_tip'          => true,
		'class'             => 'short',
		'custom_attributes' => array(
			'step' 	=> 1,
			'min'	=> 0,
		),
		'value' => $max_discount ? $max_discount : '',
	) );
	echo '</div>';
 
}
 
add_action( 'woocommerce_coupon_options_save', 'woocommerce_coupon_options_save', 10, 2 );
function woocommerce_coupon_options_save( $coupon_id, $coupon ) {
 
	update_post_meta( $coupon_id, '_max_discount', wc_format_decimal( $_POST['max_discount'] ) );
 
}

Above code will produce what is shown in the video below.

Above is just setting in the maximum discount for coupon. But does it really works?

Uhmm no! At least not yet. Below is the code that will do that. Also following is a video showing how it should look like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 = get_post_meta( $coupon->get_id(), '_max_discount', true );
 
	if ( is_numeric( $max_discount ) && ! 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;
}

The idea really is simple. Calculate the discount for both using the percent discount and a fixed discount using the max discount value. In turn whichever is the smaller use it. And taddaaa!! You have your maximum discount for your coupon. Buy me coffee if this helps you!

You may also like...

4 Responses

  1. Josh says:

    Hello,

    This is great! However I am having an issue. I was able to get it all added but when I go to save the max discount field the max discount does not save.

    Any ideas?

    • Reigel Gallarde says:

      Hello Josh, I’m not really sure. I’ve tried it now and the code works fine on the latest version of WooCommerce.

      • Josh says:

        Hello,

        Looks like it is not working with Woocommerce 2.6.1.4 but it does 2.7.

        Any ideas?

        Thanks!

      • Reigel Gallarde says:

        Hello Josh,
        I have written this code for WooCommerce 3.0 and up… So I’m not really sure with 2.6… You need to check it’s corresponding filter and action. Same as how I did it on 3.0. If I will get the time, I will try to check. But for now, I can’t suggest anything as I’m currently full of work.

Leave a Reply

Your email address will not be published. Required fields are marked *