如何在WooCommerce 3 +

时间:2018-03-09 12:31:59

标签: php wordpress woocommerce

我需要创建一个函数,将函数返回给我的一个插件WooCommerce Dymo Print。它需要$order_id$label$object$object是我需要将值返回的字段。

我创建了以下代码:

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嵌套在一个数组中而我没有正确地获取它。

元数据看起来应该是这样的。

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是我需要返回的地方

1 个答案:

答案 0 :(得分:5)

使用以下代码解决它:

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