Hide free delivery option under fixed amount

I'm building a shop using woo commerce and want to offer free delivery on orders over £30.

I've set up flat rate shipping @ £2.80 per order, and then free delivery on orders over £30, I've also added a filter to my functions file that hides the flat rate option when FREE delivery is available, which works great.

My problem is that when an order is under £30, you can still see the free delivery option. I'd like a filter that hides free delivery when the flat rate option is available.

Hope that makes sense.

This is the code I used to hide standard delivery when free is available...

// Hide standard shipping option when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );

/**
 *  Hide Standard Shipping option when free shipping is available
 *
 * @param array $available_methods
 */
function hide_standard_shipping_when_free_is_available( $available_methods ) {

    if( isset( $available_methods['free_shipping'] ) AND isset( $available_methods['flat_rate'] ) ) {

        // remove standard shipping option
        unset( $available_methods['flat_rate'] );
    }

    return $available_methods;
}
  • Vaughan

    Hi Aaron,

    Hope you're well?

    As woocommerce is not one of our products, we are not familiar with the code or filters for woocommerce at all, you would be best asking over at woothemes support for help with this as they would most likely be able to tell you straight away as they know their plugin far better than we do.

    Saying that, after a little searching, the following might work, but I can't guarantee it.

    // Hide standard shipping option when free shipping is available
    add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );
    
    /**
     *  Hide Standard Shipping option when free shipping is available
     *
     * @param array $available_methods
     */
    function hide_standard_shipping_when_free_is_available( $available_methods ) {
        global $woocommerce;
    
        $total = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
        if(isset( $available_methods['free_shipping'] && $total < 30) {
            unset( $available_methods['free_shipping'] );
        }
    
        if( isset( $available_methods['free_shipping'] ) && isset( $available_methods['flat_rate'] )) {
    
            // remove standard shipping option
            unset( $available_methods['flat_rate'] );
        }
    
        return $available_methods;
    }

    Hope this helps

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.