Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get order meta from array in WooCommerce 3+

I need to create a function that returns a value to one of my plugins WooCommerce Dymo Print. It requires $order_id, $label and $object. The $object is the field I need to return the value to.

I created the following code:

add_filter('wc_dymo_order_function', 'wpf_dymo_order_output',10,3);
function wpf_dymo_order_output( $order_id, $label, $object ) {

    if($label=='shipping' && $object='DEL_DATE') {

        //Get order from order ID
        $order=wc_get_order($order_id);
        $del_date='';

        $order_data = $order->get_data(); // The Order data
        $order_del_date = $order_data['delivery_date'];

        //Return a list (string) of all product inside this order, make sure it's well formatted before printing.
        return $order_del_date;
    } else {
        //Return order_id if is not correct label and object
        return '';
    }
}

It doesn't seem to work however and I think it's because the delivery_date is nested in an array and I'm not fetching it properly.

The meta data should look something like this.

Array
(
[31040763] => Array
    (
        [shipment_id] => 31040763
        [tracktrace] => 3SMYPA000000000
        [shipment] => Array
            (
                [barcode] => 3SMYPA000000000
                [pickup] => Array
                    (
                        [postal_code] => XXXAA
                        [street] => STRAAT
                        [city] => STAD
                        [number] => XX
                        [location_name] => Gamma
                    )
                [options] => Array
                    (
                        [signature] => 0
                        [delivery_date] => 2018-03-10 00:00:00
                    )
            )
    )
)

Where the delivery_date is what I need to return

like image 533
johnny538 Avatar asked Mar 09 '18 12:03

johnny538


People also ask

How do I get the meta order in WooCommerce?

On this order received page you will see the various order details and these are the meta data along with other details that may be saved as the meta data in the WordPress database. You can also create a custom order received or WooCommerce thank you page using the WooCommerce Redirect after checkout plugin.

How do I order an order ID in WooCommerce?

You have access to $order_id variable If you have access to the order ID (once again, usually the do_action or apply_filters might give you this), you have to get the order object first. Then do the exact same things as above. $order = wc_get_order( $order_id ); // Now you have access to (see above)...

How do I get post meta value in WordPress?

WordPress provides us with functions with which we can read the post meta associated with a post. To read the post meta of a post you can use the WordPress function get_post_meta. The get_post_meta function takes in three arguments the first is the post ID of the post whose meta you want to read.

What is WooCommerce meta data?

In the eCommerce industry, “meta data” refers to information that describes other data. In the context of WooCommerce, meta data can include information such as product prices, product descriptions, and product image URLs. This information is used by WooCommerce to display products on your website.


1 Answers

Solved it with the following code:

    $order=wc_get_order($order_id);

    $order_data = $order->get_meta('_shipments');
    $final_array = array_values($order_data);
    $order_del_date = $final_array[0]['options']['delivery_date'];

    return $order_del_date;
like image 113
johnny538 Avatar answered Nov 01 '22 20:11

johnny538