如何从 WooCommerce 中的数组中获取订单元数据 3+ [英] How to get order meta from array in WooCommerce 3+

查看:29
本文介绍了如何从 WooCommerce 中的数组中获取订单元数据 3+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个函数来为我的插件之一返回一个值WooCommerceDymo 打印.它需要 $order_id$label$object.$object 是我需要返回值的字段.

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.

我创建了以下代码:

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

但是它似乎不起作用,我认为这是因为 delivery_date 嵌套在一个数组中,而我没有正确获取它.

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.

元数据应该是这样的.

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
                    )
            )
    )
)

delivery_date 是我需要返回的地方

Where the delivery_date is what I need to return

推荐答案

用以下代码解决:

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

这篇关于如何从 WooCommerce 中的数组中获取订单元数据 3+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆