最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling calculate_shipping() with ajax in woocommerce

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论