4

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' => 'father@test.com',
  ),
  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' => 'test@gmail.com',
  ),
  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]);
        }  

}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
Nickey K
  • 71
  • 8
  • 1
    Have you tried with [wp_list_pluck](https://developer.wordpress.org/reference/functions/wp_list_pluck/)? – Cadu De Castro Alves Feb 13 '20 at 18:06
  • Amazing i'm getting all the values as a simple array, i would have never thought to find this! going to fix this completely and post the solution. Thank you @CaduDeCastroAlves – Nickey K Feb 13 '20 at 18:24

2 Answers2

1

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) "father@test.com"
  /* … */
}

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

Xhynk
  • 13,513
  • 8
  • 32
  • 69
  • 1
    Thanks for this, i got to the solution by making it into single dimensional as well! I didnt even know WordPress had wp_list_pluck(); Your answer explained this even further, thank you!! No more late night coding for tonight hah! – Nickey K Feb 13 '20 at 18:28
  • You're welcome! `wp_list_pluck()` is definitely one of those very helpful functions that doesn't get enough attention - WordPress is littered with handy little things like that: `absint()`, `maybe_unserialize()`, etc – Xhynk Feb 13 '20 at 18:53
0

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

    }

}
Nickey K
  • 71
  • 8