0

I have taken a print_r of the variable and want access some information from it, heres a preview of the print_r,

WC_Product_Variation Object
(
    [variation_data] => Array
        (
            [attribute_pa_colour] => blue
            [attribute_pa_size] => large
        )
    [sku] => BLUEANDLARGE345
    [stock] => 345
)

So if i use the following code:

<td><?php echo $product->sku; ?></td>
<td><?php echo $product->stock; ?></td>

This will echo:

BLUEANDLARGE345
345

How can i echo the variation_data array?

I have tried the following and the output is 'Array':

<td><?php echo $product->variation_data; ?></td>

EDIT:

@pp19dd

So i have tried the following:

<td><?php echo implode(", ", $product->variation_data ); ?></td>

And now the output is:

blue,         Test Product    Test Product    123
, pink        Test Product    Test Product    456
yellow,       Test Product    Test Product    789
blue, large   Test Product    Test Product    345

The code seems to put a comma after and before, which from the example is not needed.

The blue, pink, yellow, e.t.c is the [variation_data] array.

4
  • can you var_dump($product->variation_data) ?? Commented Oct 10, 2012 at 21:33
  • Sure, array(2) { ["attribute_pa_colour"]=> string(4) "blue" ["attribute_pa_size"]=> string(5) "large" } :) Commented Oct 10, 2012 at 21:47
  • where are you getting Test Product Test Product 123 from ????? Commented Oct 10, 2012 at 21:49
  • Sorry, didn't know the data might be empty. Provided a workaround in an edit. Commented Oct 10, 2012 at 21:54

2 Answers 2

2

If you want to echo the structure of the array, you could use print_r again:

print_r($product->variantion_data);

If you want to echo the individual elements of the array:

foreach($product->variantion_data as $value)
    echo $value;

Or if you want to access elements by key:

echo $product->variation_data['attribute_pa_size'];
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Joost, i have tried the foreach loop and it works great! How would i put a comma if there is more information in the $product->variantion_data variable?
0

<td><?php echo implode(", ", $product->variation_data ); ?></td>

Edit: in case you need granular bits, you can target them directly:

<td><?php echo $product->variation_data['attribute_pa_colour']; ?></td>

Edit #2: since the data might be empty, you need to filter it out. A quick and dirty way to do it:

<?php echo implode(", ", array_diff( $product->variation_data, array("") ) ) ?>

Trick: array_diff() is supposed to show you differences between arrays. So if one array is just an empty string, this method will give you everything that's not empty.

If there are random spaces in the empty data, then you can't use implode().

2 Comments

Hey, thank you, this almost works, i have made some edits to the question.
Thankyou so much! This is a charm :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.