2

I have a lot of information I need from the woocommerce function wc_get_order, below is some of it.

// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );

$order_data = $order->get_data(); // The Order data

$order_id = $order_data['id'];
$order_parent_id = $order_data['parent_id'];
$order_status = $order_data['status'];
$order_currency = $order_data['currency'];
$order_version = $order_data['version'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method_title = $order_data['payment_method_title'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method = $order_data['payment_method'];

How do I put all of it in an array and then JSON_ENCODE?

Do I need to do each value separately like

$order_array = array("order id: " . $order_id . ", order currency:" . $order_currency . ", order_version: " . $order_version );

or is there a way to loop through an put an array for key:value for each piece of info?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
LTech
  • 1,677
  • 5
  • 29
  • 48

1 Answers1

3

It depend if you want to customize te keys or not and what is the required output:

  1. JSON ENCODE: Keeping all the data as it is:

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    
    // Get the meta data in an unprotected array
    $order_data = $order->get_data();
    
    // Encoding in json
    $json_order_data = json_encode($order_data);
    
  2. JSON ENCODE: Customizing the data, keeping just some data and changing the array keys:

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    
    $order_data = []; // Initializing
    
    // Set the necessary data in an array with custom keys 
    // using WC_Abstract_Order and WC_Order methods
    $order_data['order_id']       = $order->get_id();
    $order_data['parrent_id']     = $order->get_parent_id();
    $order_data['status']         = $order->get_status();
    $order_data['order_currency'] = $order->get_currency();
    $order_data['order_version']  = $order->get_version();
    $order_data['order_payment_method']       = $order->get_payment_method();
    $order_data['order_payment_method_title'] = $order->payment_method_title();
    ## And so on…
    
    // Encoding in json
    $json_order_data = json_encode($order_data);
    
  3. A FORMATTED STRING: Customizing the data and keeping just some data:

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    
    $order_data = []; // Initializing
    
    // Set the necessary data in an array using WC_Abstract_Order and WC_Order methods
    $order_data[] ='order_id: ' . $order->get_id();
    $order_data[] ='parrent_id: ' . $order->get_parent_id();
    $order_data[] ='status: ' . $order->get_status();
    $order_data[] ='order_currency: ' . $order->get_currency();
    $order_data[] ='order_version: ' .  $order->get_version();
    $order_data[] ='order_payment_method: ' .  $order->get_payment_method();
    $order_data[] ='order_payment_method_title: ' .  $order->payment_method_title();
    ## And so on…
    
    // Convert to coma separated string
    $str_order_data = implode( ', ' . $order_data );
    

Related: How to get WooCommerce order details

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399