在 Woocommerce 3 中获取订单总运费

Get the order total shipping in Woocommerce 3

我正在尝试获取 WooCommerce 订单的送货方式或运费 - 我正在编写一个自定义电子邮件模板,该模板因免费送货和付费送货而异。

我找到了一个名为 get_total_shipping() 的函数,但现在已弃用,我找不到替代品 - 是否存在?

我注意到运费存储在一个隐藏的元字段 (_order_shipping) 中,我可以访问它,但我担心这可能会在未来的 WooCommerce 更新中中断。

自从 Woocommerce 3 get_total_shipping() method is replaced by get_shipping_total()

所以实际上有2个可用的CRUD getters methods for shipping totals in WC_Abstract_Order Class that can be used on the WC_Order实例对象:

  • get_shipping_total()即不含税的运费总额
  • get_shipping_tax() 即运费总计

因此您将以这种方式将它们与 $order 变量对象一起使用:

$shipping_total = $order->get_shipping_total();
$shipping_tax   = $order->get_shipping_tax();

还有 get_shipping_to_display() 方法可以输出格式化的运费总额。