1

I'm creating a shipping method where the customer can choose a time for the delivery to happen. This choice can be made on product page, cart and checkout. When chosen, it updates a session value with ajax.

The problem however is that I need to re-calculate shipping on cart and checkout if the time is changed, but woocommerce is not calculating shipping unless items are added or removed from the cart.

It wont even recalculate if i refresh the page, I have to manually change the content of the cart for it to fire.

Any suggestions?

  • 1
    Could you provide the related code for the shipping method, or at least a link to it… Thank you. – LoicTheAztec Apr 10 at 1:20
2

I also have the same issue when I created a shipping method and what I did is on my function for the ajax call I add something just to have an update on the cart:

global $woocommerce;
$packages = $woocommerce->cart->get_shipping_packages();
foreach( $packages as $package_key => $package ) {
    $session_key  = 'shipping_for_package_'.$package_key;
    $stored_rates = WC()->session->__unset( $session_key );
}
1

Please try adding this where needed:

WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();

This is how WooCommerce handles it in its own Ajax calls.

I found this in WC_Ajax::update_order_review() (class-wc-ajax.php:354).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.