Ad

How Do I Get Order Nested Meta Data In WooCommerce 3

We have installed another plugin for WooCommerce called Booster Plus for WooCommerce and this plugin can modify the checkout page by paying for an order by invoice number.

I am customizing our thank you page by displaying the invoice number too. Currently, I am not able to do that because I don't know how can I properly get the value of the nested $order->get_data() result.

<?php 
  $order_data = $order->get_data();
  print_r($order_data);
?>

The result of order_data above looks like below:

(
    [id] => 7403
    [discount_total] => 0
    [discount_tax] => 0
    [shipping_total] => 0.00
    [shipping_tax] => 0
    [cart_tax] => 2.47
    [total] => 21.47
    [total_tax] => 2.47
    [customer_id] => 20
    [order_key] => wc_order_8pt3q7T79
    [billing] => Array
    (
      [first_name] => John
      [last_name] => Done
      [company] => g2x
      [address_1] => 3134 James Street
      [address_2] => 
      [city] => Moose Factory
      [state] => ON
      [postcode] => P0L 1W0
      [country] => CA
      [email] => [email protected]
      [phone] => 705-658-2112
    )
    [cart_hash] => 087347d19dff4677dc8kaeb2b2c653c6
    [number] => 7403
    [meta_data] => Array
    (
      [0] => WC_Meta_Data Object
        (
          [current_data:protected] => Array
              (
                  [id] => 102652
                  [key] => mailchimp_woocommerce_campaign_id
                  [value] => 
              )

          [data:protected] => Array
              (
                  [id] => 102652
                  [key] => mailchimp_woocommerce_campaign_id
                  [value] => 
              )
        )

      [1] => WC_Meta_Data Object

      [2] => WC_Meta_Data Object

      [3] => WC_Meta_Data Object

      [4] => WC_Meta_Data Object

      [5] => WC_Meta_Data Object
      (
        [current_data:protected] => Array
        (
          [id] => 102694
          [key] => _wcj_custom_payment_gateway_input_fields
          [value] => Array
          (
            [pay_by_po] => 123456789
          )
        )

        [data:protected] => Array
        (
          [id] => 102694
          [key] => _wcj_custom_payment_gateway_input_fields
          [value] => Array
            (
              [pay_by_po] => 123456789
            )
        )
      )
    [coupon_lines] => Array
    ()

)

Do you know how can I get the value of [pay_by_po] which is 123456789? Any help is greatly appreciated. Thank you.

Ad

Answer

You can get and unprotect this nested meta data using WC_data method get_meta_data(), which gives an array of WC_Meta_Data Objects:

$meta_data = $order->get_meta_data();
print_r($order_data);

Then on each WC_Meta_Data Object, you can use WC_Meta_Data available methods like get_data() that gives an unprotected data array:

foreach( $order->get_meta_data() as $meta_data_obj ) {
    $meta_data_array = meta_data_obj->get_data();
    print_r($meta_data_array);

    $meta_key   = $meta_data_array['key']; // The meta key
    $meta_value = $meta_data_array['value']; // The meta value
}

You can also get directly any nested meta data from the order using WC_Data method get_meta() from aspecific meta key as follow:

$meta_value = $order->get_meta('_wcj_custom_payment_gateway_input_fields');
print_r($meta_value);

Note This nested meta data exist since WooCommerce version 3.


About Abstract WC_Data Class

Its Implemented by classes using the same CRUD(s) pattern.

Direct known subclasses:
WC_Abstract_Legacy_Order, WC_Abstract_Legacy_Product, WC_Customer_Download, WC_Customer_Download_Log, WC_Legacy_Coupon, WC_Legacy_Customer, WC_Legacy_Payment_Token, WC_Legacy_Shipping_Zone, WC_Legacy_Webhook, WC_Order_Item

Indirect known subclasses:
WC_Abstract_Order, WC_Coupon, WC_Payment_Token, WC_Payment_Token_CC, WC_Payment_Token_ECheck, WC_Product, WC_Product_External, WC_Product_Grouped, WC_Product_Simple, WC_Product_Variable, WC_Product_Variation, WC_Shipping_Zone, WC_Customer, WC_Webhook, WC_Order, WC_Order_Item_Coupon, WC_Order_Item_Fee, WC_Order_Item_Product, WC_Order_Item_Shipping, WC_Order_Item_Tax, WC_Order_Refund

See: Developing using WooCommerce CRUD objects

Ad
source: stackoverflow.com
Ad