woocommerce checkout update shipping value more th

2019-06-07 15:20发布

Woocommerce allows the use of the code below to update the shipping cost.

$('body').trigger('update_checkout', { update_shipping_method: true });

Am using a custom shipping plugin and am able to update the cost through ajax and eventually update my total. The problem is, the update_checkout can only work when the billing_address_1, billing_city, shipping_city and a few other fields have been changed. So I have to do something like below:

$("#billing_address_1").trigger("keypress").val(function(i,val){return val + ' -';});
$('body').trigger('update_checkout', { update_shipping_method: true }); 

Is there a better way to achieve this, other than make the form dirty for woocommerce to update the shipping cost?

Thanks in advance!!

1条回答
smile是对你的礼貌
2楼-- · 2019-06-07 16:01

This by design of woocommerce. This scripts assumes that update is needed when changing address or country.

  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
  */

If you want to make things work add this (to plugin file or functions.php):

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

Quotation from: https://gist.github.com/neamtua/bfdc4521f5a1582f5ad169646f42fcdf

For reson why, read this ticet: https://github.com/woocommerce/woocommerce/issues/12349

查看更多
登录 后发表回答