admin管理员组

文章数量:1431912

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?

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?

Share Improve this question asked Aug 8, 2018 at 21:13 JorgenJorgen 1411 silver badge5 bronze badges 1
  • 1 Could you provide the related code for the shipping method, or at least a link to it… Thank you. – LoicTheAztec Commented Apr 10, 2019 at 1:20
Add a comment  | 

2 Answers 2

Reset to default 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 );
}

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

本文标签: Calling calculateshipping() with ajax in woocommerce