Support » Plugin: WooCommerce » Woocommerce free shipping coupon not working if unset(Hide) free shipping method

  • I want to hide free shipping options and its working fine but free shipping coupon not working.

    
    function hide_shipping_when_free_is_available($rates)
    {
        // Current value of the shopping cart
        $cart_subtotal = WC()->cart->subtotal;
        $currency = get_woocommerce_currency();
    
        $free = array();
    
        foreach ($rates as $rate_id => $rate) {
            if ('free_shipping' !== $rate->method_id) {
                $rates[$rate_id]->cost = $rates[$rate_id]->cost;
            } else if ('free_shipping' === $rate->method_id) {
                // Free shipping currency convert to euro and apply 
                //if ($currency != 'USD' && $currency != 'GBP' && $currency != 'CHF') {
                $free_shipping_amounts = get_free_shipping_amounts();
                $min = min((array)$free_shipping_amounts);
                if ($currency != 'USD' && $currency != 'GBP' && $currency != 'CHF') {
                    $min = get_exchanged_currency($currency, $min, '', '', '', '', 0);
                }
                $free_shipping    = new \WC_Shipping_Free_Shipping($rate->instance_id);
                $applied_coupons  = WC()->cart->get_applied_coupons();
    
                if ('either' === $free_shipping->requires && !empty($applied_coupons) && $cart_subtotal < $min) {
                    foreach ($applied_coupons as $coupon_code) {
                        $coupon = new WC_Coupon($coupon_code);
                        if ($coupon->get_free_shipping()) {
                            $coupon_free_ship = true;
                            break;
                        }
                    }
                }
                if ($cart_subtotal < $min && !isset($coupon_free_ship)) {
    
                    unset($rates[$rate_id]);
                    echo "Hello";
                } else {
                    if ($currency == 'CHF') {
                        unset($rates[$rate_id]);
                        return $rates;
                    }
                    $free[$rate_id] = $rate;
                    break;
                }
            }
        }
        return !empty($free) ? $free : $rates;
    }
    
    add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 100);
    
    The above code is working fine but I just want to hide free shipping methods. I tried to unset free shipping methods to hide it but free-shipping coupon code not working. coupon code applied but discount not applied.
    
    I appreciate it if someone can help me with this.
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support laceyrod a11n

    (@laceyrod)

    Automattic Happiness Engineer

    Hi there!

    I tested the code snippet you provided, and I see what you mean about Free Shipping Coupons not working.

    I would recommend just using this plugin instead as it will allow you to still use Free Shipping coupons: https://wordpress.org/plugins/wc-hide-shipping-methods/

    Hope this helps!

    Thread Starter rigal-patel

    (@rigal-patel)

    Hello @laceyrod

    I made so many customizations so I can’t use the above plugin. then the issue is when a coupon is applied I unset free shipping. I add a condition for CHF currency

    
    if ($currency == 'CHF') {
                        unset($rates[$rate_id]);
                        return $rates;
    }
    

    I unset free shipping options because I don’t want to display these options so the main issue is if we unset free shipping methods unset($rates[$rate_id]);
    then free shipping coupon not works.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Woocommerce free shipping coupon not working if unset(Hide) free shipping method’ is closed to new replies.