• Resolved samina04

    (@samina04)


    Dear WooCommerce Support,

    I hope this message finds you well. I’m currently working on a WooCommerce store and need to modify the logic that handles the “Minimum Spend” and “Maximum Spend” functionality for coupons. Specifically, I need the total cart amount to include products that do not qualify for the coupon when determining whether the minimum or maximum spend criteria are met.

    After reviewing the core WooCommerce file WC_Discounts (located in woocommerce/includes/class-wc-discounts.php), I see that the apply_coupon and validate_coupon_minimum_amount functions are responsible for handling this logic. However, I understand that directly editing core WooCommerce files is not recommended as it could lead to issues during future updates.

    Could you please advise on the safest approach to modify this behavior? Is there a specific action, filter, or hook available that would allow me to extend or override this functionality without directly modifying the core files? Alternatively, is there another recommended method (such as extending the class via a plugin) that I can use to safely implement this custom logic?

    I look forward to your guidance on how to proceed.

    Thank you for your assistance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support ckadenge (woo-hc)

    (@ckadenge)

    Hello @samina04,

    Thank you for reaching out.

    I appreciate your thoughtful approach to modifying the “Minimum Spend” and “Maximum Spend” functionality for coupons.

    It’s not advisable to modify core WooCommerce files directly. Instead, you can use hooks and filters to extend or override this functionality. This allows you to make changes without worrying about future updates overwriting your custom code.

    The specific hook you’ll need to use will depend on the exact functionality you want to modify. For example, you might use the woocommerce_coupon_get_discount_amount filter to modify the discount amount based on your custom logic.

    However, as this will involve custom coding, I suggest reaching out to a developer, since such customization requests are out of our support scope.

    The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

    Thread Starter samina04

    (@samina04)

    Okay, so is this safe to use –


    add_filter(‘woocommerce_coupon_is_valid’, ‘custom_check_cart_amount_for_coupon’, 10, 2);
    function custom_check_cart_amount_for_coupon($valid, $coupon) {
    if (!$valid) {
    return $valid; /
    }


    $min_spend = $coupon->get_minimum_amount();
    $max_spend = $coupon->get_maximum_amount();

    $cart_total = WC()->cart->get_subtotal();

    if (!empty($min_spend) && $cart_total < $min_spend) {
    wc_add_notice(sprintf(__('The minimum spend for this coupon is %s.', 'woocommerce'), wc_price($min_spend)), 'error');
    return false;
    }

    if (!empty($max_spend) && $cart_total > $max_spend) {
    wc_add_notice(sprintf(__('The maximum spend for this coupon is %s.', 'woocommerce'), wc_price($max_spend)), 'error');
    return false;
    }

    return $valid;

    }

    Plugin Support ckadenge (woo-hc)

    (@ckadenge)

    Hi @samina04,

    Thanks again for reaching out.

    The code seems to meet your desired requirements, however, I’m unable to tell if it is safe or not as we do not provide support for custom code.

    As a best practice, you can try it out on a staging site to see if it causes any errors in your store. Please be sure to have a backup in place.

    All the best.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.