从Woocommerce的订单中获取本地取件以及详细信息

时间:2019-03-29 13:47:52

标签: php wordpress woocommerce orders shipping-method

在Woocommerce中,我只想获取“本地取件”运输详细信息以显示在自定义电子邮件中。我在下面的函数中尝试过,但是它们对于“本地拾取”没有显示任何内容。

我可以使用哪个功能来获取“本地取件”信息?

我尝试了以下WC_Order方法,但未成功:

  • $order->get_shipping_address_1()
  • $order->get_formatted_shipping_address()

修改:

抱歉,我没有提到,但是我正在使用Local Pickup Plus插件


编辑2:

这是我获取本地取件加插件docs.woocommerce.com/document/local-pickup-plus的本地取件信息的方法,它将元数据放入主订单变量中。

$order = wc_get_order( $order_id );

foreach ($order->get_data() as $key => $value):
        if ($key==='shipping_lines'):
            foreach ($value as $k=>$v):
                $a = $v->get_meta_data();
                foreach ($a as $x=>$y):
                    $t = $y->get_data();
                    $mykey = $t['key'] ;
                    $pickup["$mykey"] = $t['value'];
                endforeach;
            endforeach;
        endif;
endforeach;

然后您可以使用以下变量:

$pickup['_pickup_location_name']
$pickup['_pickup_location_address']['address_1']
$pickup['_pickup_location_phone']['address_2']
$pickup['_pickup_location_address']['postcode']
$pickup['_pickup_location_address']['city']
$pickup['_pickup_location_address']['state'] $pickup['_pickup_location_address']['country']
$pickup['_pickup_location_phone']
$pickup['_pickup_date']
$pickup['_pickup_minimum_hours']

1 个答案:

答案 0 :(得分:1)

有关订购商品的运输详细信息,请参见:Get orders shipping method details in WooCommerce 3

要定位WC_Order对象中的订单运送行详细信息,可以使用以下代码:

// Loop though order items shipping
foreach( $order->get_shipping_methods() as $item_id => $item ){
    $shipping_item_name          = $item->get_name();
    $shipping_item_type          = $item->get_type();
    $shipping_method_title       = $item->get_method_title();
    $shipping_method_id          = $item->get_method_id();
    $shipping_method_instance_id = $item->get_instance_id();
    $shipping_method_total       = $item->get_total();
    $shipping_method_total_tax   = $item->get_total_tax();
    $shipping_method_taxes       = $item->get_taxes();

    // Get custom meta-data
    $formatted_meta_data = $item->get_formatted_meta_data( ' ', true );

    // Displaying the row custom meta data Objects (just for testing)
    echo '<pre>'; print_r($formatted_meta_data); echo '</pre>';
}
  

关于自定义送货元数据:

     

您可以使用WC_Data方法get_meta()从位于任何自定义元数据对象中的自定义元key中访问它,例如:

$value = $item->get_meta('the_custom_key'); // 'the_custom_key' need to be replaced by the meta "key".

注意:在大多数Woocommerce 电子邮件模板与电子邮件通知相关的挂钩中,您可以使用WC_Order对象,因为它是全局对象包括在内。如果没有,您可以从订单ID中获取它,例如:

$order = wc_get_order( $order_id );

与订单相关的线程:


添加-适用于本地Pickup Plus插件

似乎您使用的是Local Pickup Plus plugin,它会在运输行中添加特定的自定义元数据。

// Loop though order items shipping
foreach( $order->get_shipping_methods() as $item_id => $item ){
    $location_id        = $item->get_meta('_pickup_location_id');
    $location_name      = $item->get_meta('_pickup_location_name');

    $location_address   = $item->get_meta('_pickup_location_address'); // Array
    $location_address_1 = $location_address['address_1'];
    $location_address_2 = $location_address['address_2'];
    $location_postcode  = $location_address['postcode'];
    $location_city      = $location_address['city'];
    $location_state     = $location_address['state'];
    $location_country   = $location_address['country'];

    $location_phone     = $item->get_meta('_pickup_location_phone');

    $pickup_date        = $item->get_meta('_pickup_date');
    $pickup_min_hours   = $item->get_meta('_pickup_minimum_hours');
}