如何访问当前的woocommerce订单送货方式ID?

bel*_*rt1 15 wordpress shipping woocommerce

我找到了方法

$order->get_shipping_method() 
Run Code Online (Sandbox Code Playgroud)

访问该名称,但我想检索id而不是名称?

bel*_*rt1 21

我想如果有人遇到和我一样的问题,我会分享如何解决这个问题.我在$ order变量中有WC_Order.

$order->get_items( 'shipping' );
Run Code Online (Sandbox Code Playgroud)

这给了我一个包含name,type,method_id,cost和tax的数组.

  • `reset($ order-> get_items('shipping')) - > get_method_id();`诀窍,如果你只需要获取id.`get_items()`为您提供关联的运输项目数组.`reset()`接受这个数组的第一项,即`WC_Order_Item_Shipping`.最后`get_method_id()`获取id(例如``local_pickup:42`). (4认同)

And*_*eyP 7

$shipping_method = @array_shift($order->get_shipping_methods());
$shipping_method_id = $shipping_method['method_id'];
Run Code Online (Sandbox Code Playgroud)