
Setting max discount for coupons on WooCommerce is easy if you have this
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.
[html] 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; } [/html]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.