2
votes

In WooCommerce 3, I have these shipping options (settings):

  1. Free Shipping: free_shipping:1 - Minimum order amount is set at $50.
  2. Normal Shipping flat_rate:3 - Amount $5.
  3. Express Shipping flat_rate:5 - Amount $10.

I would like Express Shipping option to be always available (shown).

But when Free shipping is available (meaning that the customer has more than $50 in the cart) I would like to hide Normal Shipping only.

So when Free shipping is NOT available (and hidden), the available shipping rates will be Normal Shipping and Express Shipping.

Is that possible? How can I get this on WooCommerce 3?

1
This plugin will make this job easier - elextensions.com/plugin/…YajiV

1 Answers

5
votes

Based on the official WooCommerce snippet code, making some light changes, you will be able to hide only your first flat rate when free shippings is available:

add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
    // HERE yours 2nd flat rate "Express Shipping" (that you never hide) in the array:
    $flat_rates_express = array( 'flat_rate:5', 'flat_rate:12', 'flat_rate:14' );

    $free = $flat2 = array();
    foreach ( $rates as $rate_key => $rate ) {
        // Updated Here To 
        if ( in_array( $rate->id, $flat_rates_express ) )
            $flat2[ $rate_key ] = $rate;
        if ( 'free_shipping' === $rate->method_id )
            $free[ $rate_key ] = $rate;
    }
    return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested on WooCommerce 3 and works.

Refresh the shipping caches:
1) First empty your cart.
2) This code is already saved on your function.php file.
3) Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.