Set Max Coupon Discount Amount in WooCommerce

No matter what’s the cart amount is you can set max amount for coupon. Here is PHP code for your theme function.php file and it works If you are giving 10% discount but want user have any numbers of items or amount in cart but still max discount they get is 100 only.

Like shown in image below

Set Max Coupon Discount Amount in WooCommerce

Step 1 – Set Max Coupon Discount Amount in WooCommerce

Open theme function.php file and paste below code to add function in wp-admin area. Make sure before you edit any file take backup first.

add_action( 'woocommerce_coupon_options_usage_limit', 'woocommerce_coupon_options_usage_limit', 10, 2 );
 function woocommerce_coupon_options_usage_limit( $coupon_id, $coupon ){
     echo '
 ';
     // 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 '
 ';
 }
 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'] ) );
 }

Now a field added to your wp-admin area in coupon section

Step 2 – Set Max Coupon Discount Amount in WooCommerce

Now you need to make this function work on website order side. Copy below code and paste in function.php after above code you have already pasted.

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;
}

That’s all your code should be works now.

Sandy

Sandy

2 thoughts on “Set Max Coupon Discount Amount in WooCommerce

  1. Hi, I have tested this code. Its not working properly. Even i tried changing it myself. It worked pretty well but it works on different product individually on cart. Lets say max discount is 100 and we have multiple products in cart. It gives $100 discount on every product. Can you please check this at your end and update here if possible?

  2. our PHP code changes were rolled back due to an error on line 142 of file /bitnami/wordpress/wp-content/themes/diza/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘,’

Leave a Reply

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

Name *
Email *