1

I am using Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3 lightly changed answer code to hide all shipping methods except one. The only method I want showing is a rate from the "Woocommerce Advanced Shipping" plugin.

I am using the correct rate ID etc...

Everything works fine except when a customer tries to click that shipping method, it won't stay selected. It just jumps back to free shipping.

I have tried debugging and also tried the code with a native woocommerce flat rate ID and it showed up/able to select it just fine.

add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {

    $flat_rates_express = array( '2588' );

    $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:12' === $rate->id )
            $free[ $rate_key ] = $rate;
    }
        return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}

ID I want to Keep Shown: "2588" (Custom Shipping Rate From Plugin)

How can I disable the Flat rate shipping method when free shipping is available o and keep a custom shipping rate (from a plugin)?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Your code works, but the problem is `'2588'` that is not the correct shippnig rate id reference… see [this screenshot](https://i.stack.imgur.com/AlATr.png) that shows how to get the correct shipping rate ID using the browser code inspector. – LoicTheAztec Jan 04 '19 at 13:44
  • @LoicTheAztec Here is screenshot showing value as "2588" am i supposed to use "shipping_method_0_2588" Instead? https://gyazo.com/35ae5e3f9cd9f1095fbc0391423c0bec – Fractal Samurai Jan 04 '19 at 23:42
  • Your shipping plugin is making very curious shipping method rate IDs… It seems to be `2588` (the value), but not `shipping_method_0_2588` which is the DOM html tag ID. Your code works (tested with woocommerce shipping methods), but may be your plugin shipping method rate Id doesn't… – LoicTheAztec Jan 04 '19 at 23:52
  • @LoicTheAztec Yeah it works just fine with native woocom shipping rates. As for the plugin shipping rate, the base function works fine as well (it hides all other rates). But it just doesn't let me click it. Is there a way to find an answer without contacting the dev? – Fractal Samurai Jan 05 '19 at 00:15
  • Or @sormano Maybe you could chime in? – Fractal Samurai Jan 05 '19 at 00:16
  • @LoicTheAztec Is there a way to hide one specific flat rate and show the rest instead? I only have one other flat rate (the one that becomes free). Just in case there is no solution – Fractal Samurai Jan 05 '19 at 00:46
  • I have just answered with something a bit different that should works. – LoicTheAztec Jan 05 '19 at 01:18

1 Answers1

2

As you have 3 shipping methods, 1 free shipping, 1 flat rate and 1 custom '2588', it's possible to hide the flat rate shipping method when free shipping is available instead:

add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate', 1000, 2 );
function free_shipping_disable_flat_rate( $rates, $package ) {
    // Here your free shipping rate Id
    $free_shipping_rate_id = 'free_shipping:12';

    // When your Free shipping method is available
    if ( array_key_exists( $free_shipping_rate_id, $rates ) ) {
        // Loop through shipping methods rates
        foreach ( $rates as $rate_key => $rate ) {
            // Removing "Flat rate" shipping method
            if ( 'flat_rate' === $rate->method_id ){
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches:

  1. This code is already saved on your function.php file.
  2. In a shipping zone settings, disable / save any shipping method, then enable back / save.
    You are done and you can test it.
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Absolutely amazing thanks so much! I've came across your contributions before and these parts wouldn't be the same without you brother! – Fractal Samurai Jan 05 '19 at 03:03