2

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • This plugin will make this job easier - https://elextensions.com/plugin/conditionally-hide-woocommerce-shipping-methods-plugin/ – YajiV Apr 09 '19 at 18:11

1 Answers1

5

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello Loic, Thank you so much for your help. You don't know how appreciative I am to your kind assistance. :) It's working perfectly for my Zone 1. However for my Zone 2 and Zone 3 areas, it's only showing Free Shipping but not the Express Shipping options. I believe it's because for each Shipping Zones, each Express Shipping has a different Rate ID. For example, Zone 2's Express Shipping Rate ID is flat_rate:12 and Zone 3's Express Shipping Rate ID is flat_rate:14. How do I change the code or my settings so that this will conditional rate will apply to all 3 Zones? Thank you! – Lex Na Wei Ming Sep 26 '17 at 01:25