woocommerce checkout 不止一次更新运费 [英] woocommerce checkout update shipping value more than once

查看:32
本文介绍了woocommerce checkout 不止一次更新运费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Woocommerce 允许使用下面的代码来更新运费.

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

我正在使用自定义运输插件,并且能够通过 ajax 更新成本并最终更新我的总数.问题是,update_checkout 只能在 billing_address_1、billing_city、shipping_city 和其他一些字段发生更改时才能工作.所以我必须做如下事情:

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

有没有更好的方法来实现这一点,除了让 woocommerce 的表格变脏以更新运费?

提前致谢!!

解决方案

这是 woocommerce 的设计.此脚本假定更改地址或国家/地区时需要更新.

 jQuery('body').trigger('update_checkout');/* 这样做是更新订单审查表,但不更新运输成本;您的运输类的calculate_shipping 函数将不会再次被调用;所以如果你像我一样,你制作了一个送货方式插件,你必须根据付款方式更改费用这是确保它做到这一点的唯一方法*/

如果你想让事情工作,添加这个(到插件文件或functions.php):

function action_woocommerce_checkout_update_order_review($array, $int){WC()->购物车->calculate_shipping();返回;}add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);

引自:https://gist.github.com/neamtua/bfdc4521f5a1562fcdf5a1562f5a1562f42f/p>

有关原因,请阅读此ticket:https://github.com/woocommerce/woocommerce/issues/12349

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!!

解决方案

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

这篇关于woocommerce checkout 不止一次更新运费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆