1

Based on "Fix maximum coupon Discount On Cart percentage in WooCommerce" answer thread, I use the following code in my active theme's function.php file:

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']));
}

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 (isset($max_discount) && is_numeric($max_discount) && ($max_discount > 0) && !is_null($cart_item) && WC()->cart->subtotal_ex_tax) {
        if($discount > $max_discount) {
            $discount = $max_discount;
        }
    }
    return $discount;
}

But this code is only active for one product in the shopping cart and does not work for several products in the cart.

What changes should I need to do to make it work for all cart items instead?

Any track is welcome.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
arsalan
  • 11
  • 2
  • The answer code where you have taken the code, doesn't work and have been badly twisted from this original thread: [WooCommerce set maximum coupon discount](https://reigelgallarde.me/programming/woocommerce-set-maximum-coupon-discount/) … As you can see this answer has been downvoted and should be deleted… Instead take the [original code](https://reigelgallarde.me/programming/woocommerce-set-maximum-coupon-discount/) from reigelgallarde.me web site. – LoicTheAztec Apr 15 '19 at 03:07
  • i test original code but if we have one QUANTITY Of every PRODUCT for example: PRODUCT a QUANTITY 1 PRODUCT b QUANTITY 1 original code work good when We change QUANTITY Of every PRODUCT for example: PRODUCT a QUANTITY 2 PRODUCT b QUANTITY 1 original code Does not work and displays the wrong result in a coupon discount – arsalan Apr 15 '19 at 10:37
  • Woocommerce 3.2 (or 3.3) has introduced big changes to coupon discounts in cart and orders, then most all related answer code that have been published before doesn't work. – LoicTheAztec Apr 15 '19 at 16:20

0 Answers0