doraemon
doraemon

Reputation: 398

Get Local pickup plus details from the order in Woocommerce

In Woocommerce, I just want to get "local pickup" shipping details to display on a custom email. I tried below functions but they don't show anything for "local pickup".

Which function I can use to get "local pickup" info?

I tried without success the following WC_Order methods:


Edit:

Sorry I did not mention that, but I am using Local Pickup Plus plugin


Edit 2:

This is how I got local pickup info for Local Pickups Plus Plugin docs.woocommerce.com/document/local-pickup-plus which puts meta data to main order variable.

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

Then you can use the variables below:

$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']

Upvotes: 2

Views: 3786

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253849

For Order items shipping details refer to: "Get orders shipping method details in WooCommerce 3"

To target order shipping lines details from the WC_Order object you can use the following code:

// 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>';
}

Regarding the custom shipping metadata:

You can access it using the WC_Data method get_meta() from the custom meta "key" located in any custom meta data Objects, like:

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

Note: In most Woocommerce email templates and email notification related hooks, you can use the WC_Order object as it's globally included. If not you can get it from the Order ID like:

$order = wc_get_order( $order_id );

Orders related threads:


Addition - For Local Pickup Plus plugin

It seems that you are using Local Pickup Plus plugin which adds specific custom meta data in the shipping lines.

// 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');
}

Upvotes: 2

Related Questions