Nickey K
Nickey K

Reputation: 71

How to get custom_meta_data

I hope someone can help, I've been trying to get information from my custom fields plugin on Woocommerce (using it with WooCommerce subscriptions).

I am trying to capture the details so I can JSON POST it to another platform.

I have managed to hook into the active status of the subscription, get the details via $subscription variable, JSON decoded it and then captured my custom fields part found in the 'meta_data' array.

This is now saved into a variable called $orderdata.

The variable looks like

array (
  0 => 
  array (
    'id' => 20992,
    'key' => '_billing_parent_first_name',
    'value' => 'Father First Name',
  ),
  1 => 
  array (
    'id' => 20993,
    'key' => '_billing_parent_last_name',
    'value' => 'Father Last Name',
  ),
  2 => 
  array (
    'id' => 20994,
    'key' => '_billing_parent_email',
    'value' => '[email protected]',
  ),
  3 => 
  array (
    'id' => 20995,
    'key' => '_billing_parent_number',
    'value' => '12345678',
  ),
  4 => 
  array (
    'id' => 20996,
    'key' => '_billing_childs_first_name',
    'value' => 'test',
  ),
  5 => 
  array (
    'id' => 20997,
    'key' => '_billing_childs_last_name',
    'value' => 'test',
  ),
  6 => 
  array (
    'id' => 20998,
    'key' => '_billing_childs_email',
    'value' => '[email protected]',
  ),
  7 => 
  array (
    'id' => 20999,
    'key' => 'is_vat_exempt',
    'value' => 'no',
  ),
  8 => 
  array (
    'id' => 21000,
    'key' => 'thwcfe_ship_to_billing',
    'value' => '1',
  ),
)

My goal is to save each field like _billing_parent_number into a variable so I can ship it off using POST.

Does anyone know of a way I can capture the fields I need?

I have tried numerous methods, from array searches to array column (I'm using error_log to test in Wordpress so I can see the result).

I got close with the foreach loop but I do not know how to get the following field in the object and get it's value, next() in this case didn't work :(

Any help is appreciated!

add_action( 'woocommerce_subscription_status_active', 'get_details_add_bundle' );

function get_details_add_bundle($subscription) {

   $obj = json_decode($subscription, true);

    error_log($obj);

    $orderData = $obj['meta_data'];

    foreach($orderData as $key => $value) {
       if($key == 'key' && $value="_billing_childs_email"){
         error_log(next($orderData)[2]);
        }  

}

Upvotes: 4

Views: 192

Answers (2)

Nickey K
Nickey K

Reputation: 71

I got the solution, thank you for the answers!

function get_details_add_bundle($subscription) {

    $obj = json_decode($subscription, true);
    $orderData = $obj['meta_data'];
    $values = wp_list_pluck( $orderData, 'key', 'value');

    foreach($values as $key => $value) {
        if ($value == 'billing_childs_email') {
          $child_email1 = $key;
        }

    }

}

Upvotes: 0

Xhynk
Xhynk

Reputation: 13890

I think you may be thinking about this is a bit too much with keys and values and such. The array you posted is an Array of Arrays. This means you can run through it with a foreach loop, and would need a subloop (or other methods) to access the keys and values - but you shouldn't even need to go that far.

If we create a new array (that you'll eventually POST, so I assume you want just key => value pairs), we can push values from the sub-arrays into it, since they're single dimensional, this makes it really easy!

$postable = array(); // Create a new array (we'll eventually POST this)

// get each sub array, one at a time
foreach( $orderdata as $data ){
    /* The $postable "key" will be the value of $data['key'], and the value
       pair for it will be the value of $data['value'] */
    $postable[$data['key']] = $data['value'];
}

Now that we've pushed each of these pairs into the $postable array, we end up with a simple key => value paired associative array:

array(#) {
  ["_billing_parent_first_name"] => string(17) "Father First Name"
  ["_billing_parent_last_name"]  => string(16) "Father Last Name"
  ["_billing_parent_email"]      => string(15) "[email protected]"
  /* … */
}

Here's a working example (using a truncated array for brevity): https://3v4l.org/kWQnB

Upvotes: 1

Related Questions