Skip to content

Instantly share code, notes, and snippets.

@chuckmo
Last active September 7, 2023 22:17
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chuckmo/9c2a57f64cf60ee0d9d3 to your computer and use it in GitHub Desktop.
Save chuckmo/9c2a57f64cf60ee0d9d3 to your computer and use it in GitHub Desktop.
Disable WooCommerce's Shipping Rates Cache
// updated per harisrozak:
add_filter('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache');
function clear_wc_shipping_rates_cache(){
$packages = WC()->cart->get_shipping_packages();
foreach ($packages as $key => $value) {
$shipping_session = "shipping_for_package_$key";
unset(WC()->session->$shipping_session);
}
}
@korkey128k
Copy link

At some point, they edited the option_name, also storing it in a serialized array, so something like this should be used now:

$wc_options = wp_parse_args( get_option( 'woocommerce_status_options', array() ), array( 'uninstall_data' => 0, 'template_debug_mode' => 0, 'shipping_debug_mode' => 0 ) );
$wc_options['shipping_debug_mode'] = 1;
update_option('woocommerce_status_options', $wc_options);

@harisrozak
Copy link

After research for 2 days, i got this solution, i think this more cleaner:

add_filter('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache');

function clear_wc_shipping_rates_cache(){
    $packages = WC()->cart->get_shipping_packages();

    foreach ($packages as $key => $value) {
        $shipping_session = "shipping_for_package_$key";

        unset(WC()->session->$shipping_session);
    }
}

@marcbovet
Copy link

👍 harisrozak ! you're my new best friend :-)

@asterion
Copy link

asterion commented Dec 9, 2017

@deyvi-james
Copy link

@harisrozak the best. Thank you!

@neoseeyou
Copy link

Hello

I tried to apply @harisrozak snippet or @DeyviJam snippet too but it's not working for me on last version of woo. Do you know how i can disable the woocommerce shipping cache plz?

@gmosornoza
Copy link

The @harisrozak code must be hooked to the woocommerce_checkout_update_order_review action, not filter.

@azharp
Copy link

azharp commented Aug 13, 2018

thank you! @harisrozak

@diegoversiani
Copy link

Thanks @harisrozak!

@laurikar
Copy link

Another method:

add_action('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache');
function clear_wc_shipping_rates_cache(){
    WC_Cache_Helper::get_transient_version( 'shipping', true );
}

@akshuvo
Copy link

akshuvo commented Sep 16, 2020

After research for 2 days, i got this solution, i think this more cleaner:

add_filter('woocommerce_checkout_update_order_review', 'clear_wc_shipping_rates_cache');

function clear_wc_shipping_rates_cache(){
    $packages = WC()->cart->get_shipping_packages();

    foreach ($packages as $key => $value) {
        $shipping_session = "shipping_for_package_$key";

        unset(WC()->session->$shipping_session);
    }
}

This is working like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment