add_filter('woocommerce_package_rates', 'conditionally_remove_free_shipping', 10, 2);
function conditionally_remove_free_shipping($rates, $package) {
    $min_subtotal = 0.01;
    $coupon_code_to_prevent = '2MC8Q43F';
    $cart_subtotal = WC()->cart->get_subtotal();
    if ($cart_subtotal >= $min_subtotal) {
        foreach ($rates as $rate_id => $rate) {
            if ('free_shipping' === $rate->method_id) {
                unset($rates[$rate_id]);
            }
        }
        wc_add_notice(__('The coupon code ' . $coupon_code_to_prevent . ' cannot be applied because this coupon can only be used when the entire payment amount is discounted with points.', 'woocommerce'), 'error');
    }
    return $rates;
}
/* ++++++++++++++++++++++++++++++++++++++++++ */
function check_and_remove_coupon() {
    $coupon_code_to_prevent = '2MC8Q43F';
    $cart_subtotal = WC()->cart->get_subtotal();
    if (WC()->cart->has_discount($coupon_code_to_prevent) && $cart_subtotal >= 0.01) {
        WC()->cart->remove_coupon($coupon_code_to_prevent);
    }
}
add_action('woocommerce_before_cart_table', 'check_and_remove_coupon');
add_action('woocommerce_checkout_update_order_review', 'check_and_remove_coupon');

By admin

Leave a Reply

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