Skip to content

Instantly share code, notes, and snippets.

@neamtua
Created September 8, 2016 06:13
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save neamtua/bfdc4521f5a1582f5ad169646f42fcdf to your computer and use it in GitHub Desktop.
Save neamtua/bfdc4521f5a1582f5ad169646f42fcdf to your computer and use it in GitHub Desktop.
WooCommerce: Update shipping costs on checkout using ajax
<script type="text/javascript">
/* in order to update info on your checkout page you need to trigger update_checkout function
so add this in your javascript file for your theme or plugin
*/
jQuery('body').trigger('update_checkout');
/* what this does is update the order review table but what it doesn't do is update shipping costs;
the calculate_shipping function of your shipping class will not be called again;
so if you were like me and you made a shipping method plugin and you had to change costs based on payment method then
this is the only way to ensure it does just that
*/
</script>
<?php
# add this in your plugin file and that's it, the calculate_shipping method of your shipping plugin class will be called again
function action_woocommerce_checkout_update_order_review($array, $int)
{
WC()->cart->calculate_shipping();
return;
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);
?>
@CaroRed
Copy link

CaroRed commented Sep 16, 2017

Thank you. This is just what I needed... You helped me a lot.

@akshuvo
Copy link

akshuvo commented Oct 15, 2018

Great Great Great (y)

@parthtechcompose
Copy link

Not working!

@web-elite
Copy link

not working for me :(

@alexandru-burca
Copy link

alexandru-burca commented Aug 28, 2020

That doesn't work anymore, use add_filter( 'woocommerce_package_rates', 'change_rates', 10, 2 ); instead. Where "change rates" is a function.

Example:
<script type="text/javascript">jQuery('body').trigger('update_checkout');</script>
and

add_filter( 'woocommerce_package_rates', 'change_rates', 10, 2 );
function change_rates( $rates, $packages ) {
  foreach ( $rates as $rate ) {
            $rate->label = "Delivery";
            $rate->cost = 40;
   }
 return $rates
}

@johnnny-bravoo
Copy link

Below code will update Shipping costs immediately when Postcode value changes.

$('#billing_postcode').on('input', function() { $('body').trigger('update_checkout', { update_shipping_method: true }); });

@OMoselufioye
Copy link

That doesn't work anymore, use add_filter( 'woocommerce_package_rates', 'change_rates', 10, 2 ); instead. Where "change rates" is a function.

Example:
<script type="text/javascript">jQuery('body').trigger('update_checkout');</script>
and

add_filter( 'woocommerce_package_rates', 'change_rates', 10, 2 );
function change_rates( $rates, $packages ) {
  foreach ( $rates as $rate ) {
            $rate->label = "Delivery";
            $rate->cost = 40;
   }
 return $rates
}

If I instead wish to invoke my custom shipping method to recalculate shipping costs, how do I do that please?

@luisdatec
Copy link

Thanks! This code jQuery('body').trigger('update_checkout'); saved me the time of research 😃

@PhongGCS
Copy link

That is not working. In source woocommerce mention to this docs
https://gist.github.com/woogists/271654709e1d27648546e83253c1a813

image

Put this code to functions.php to solve this

function wc_shipping_rate_cache_invalidation( $packages ) {
	foreach ( $packages as &$package ) {
		$package['rate_cache'] = wp_rand();
	}

	return $packages;
}
add_filter( 'woocommerce_cart_shipping_packages', 'wc_shipping_rate_cache_invalidation', 100 );

@sviatoslavadamiv
Copy link

Thanks @PhongGCS, it really works.

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