1
votes

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)?

1
Your code works, but the problem is '2588' that is not the correct shippnig rate id reference… see this screenshot that shows how to get the correct shipping rate ID using the browser code inspector.LoicTheAztec
@LoicTheAztec Here is screenshot showing value as "2588" am i supposed to use "shipping_method_0_2588" Instead? gyazo.com/35ae5e3f9cd9f1095fbc0391423c0becFractal Samurai
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
@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
Or @sormano Maybe you could chime in?Fractal Samurai

1 Answers

1
votes

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.