-1

I'm trying to get the product attributes. I can dig into the objects like this but I get stuck here: How would I get the value 1500?

Thanks

$bundle = new WC_Product_Bundle($post->ID);
$products = $bundle->get_bundled_items();
foreach ($products as $product) {
var_dump($product->{'product'}->{'attributes'});

array(1) {
  ["energy"]=>
  object(WC_Product_Attribute)#1402 (1) {
    ["data":protected]=>
    array(6) {
      ["id"]=>
      int(0)
      ["name"]=>
      string(6) "energy"
      ["options"]=>
      array(1) {
        [0]=>
        string(4) "1500"
      }
      ["position"]=>
      int(0)
      ["visible"]=>
      bool(true)
      ["variation"]=>
      bool(false)
    }
  }
}
versionz
  • 29
  • 1
  • 6

2 Answers2

0

you can try getting the "1500" figure by:

$product->['product']->['attributes']['energy']['data']['options'][0]
Tommys
  • 123
  • 7
  • 1
    `data` is a protected property. An object `getter` method would need to be used to retrieve the value, otherwise it will throw a `Cannot access protected property` Fatal error. See: https://3v4l.org/NAFUU – Will B. Jun 15 '18 at 00:45
  • Thanks this did help me solve it. I managed to get the data like this: `$product->{'product'}->{'attributes'}{'energy'}{'data'}{'options'}{0}` – versionz Jun 15 '18 at 07:58
  • @fyrye deleted my previous comment, admitting you were right. Actually you are wrong. OP is accessing the object in same class/function so protected is not enforced! In your example you have created a class where as OP is not doing that. – Tommys Jun 15 '18 at 08:46
  • hey @Tommys if you update your answer with the correct type of brackets I'll accept your answer. – versionz Jun 15 '18 at 11:07
  • @versionz as far as above code goes you are trying to get the values of array rather than property. See https://stackoverflow.com/questions/38415367/php-arrays-square-brackets-vs-curly-braces-arrayi-vs-arrayi – Tommys Jun 15 '18 at 11:21
  • Based on the code we were provided there was no way to provide an acceptable solution. This means their objet uses a __get magic method or extends arrayaccess, in either case it was impossible to know without the OP providing the code – Will B. Jun 15 '18 at 11:42
0

Here's the full code I ended up using to create a json format array. This may be helpful if anyone else needs nutrition data from optional bundled products to be averaged and displayed in a nutritional info panel/label on packaging etc...

Could be useful for recipes, smoothies, muesli mixers etc...

            $nutrition = [];
            $bundle = new WC_Product_Bundle($post->ID);
            $products = $bundle->get_bundled_items();
            foreach ($products as $product) {

                $att1 = current($product->{'product'}->{'attributes'});
                $att1shift = current($att1);
                $key = $att1shift['name'];
                $value = $product->{'product'}->{'attributes'}{$key}{'data'}{'options'}{0};

                $att2 = next($product->{'product'}->{'attributes'});
                $att2shift = current($att2);
                $key2 = $att2shift['name'];
                $value2 = $product->{'product'}->{'attributes'}{$key2}{'data'}{'options'}{0};                   

                $nutrition[$product->{'product'}->{'id'}] = array($key => $value, $key2 => $value2 );
            }
            json_encode($nutrition);
versionz
  • 29
  • 1
  • 6