0

I have this situation:

I have many configurable products with child products(the same product but with different variations). Here are the variations that I mean

enter image description here

What I need to do is just to get the dimensions of the variations (weight, length, height and width). If I do this I get 0, because the main product has no height in it's attributes, and also the attribute height is not showing in the main product, so I cannot set a height.

$height += $product->getData('ts_dimensions_height'); 

How can I set dimensions to the main product, or how can I get the attributes of the variation products?

Note: I cannot do a foreach array in the variations of the configurable product, because I'm adding up all dimensions depending on the amount of the products. For example If I have Product A with child product 2 and Product B with child product 1 I need to add up the dimensions, just like this:

foreach ($orderItems as $item) {

             $product = $item->getProduct();

                $weight += (float) ($product->getData('weight')*$item->getQty());
                $height += (float) ($product->getData('ts_dimensions_height')*$item->getQty()); 
                $length += (float) $product->getData('ts_dimensions_length');
                $width += (float) $product->getData('ts_dimensions_width');  


         }

Greetings!

0

In this case, you need to get the child item. You can use below code to get child item detail and add your logic:

 foreach( $orderItems as $item){ 
    if ($item->getProductType()=="configurable") { 
        foreach( $item->getChildrenItems() as $chitem){
             // child product
            echo $chitem->getSku(); 
        } 
    } else {
        // simple product
    }
}
| improve this answer | |
  • It's not working, I'm getting invalid value for foreach() does getChildrenItems() method exists? – Oscar Vaz Feb 24 at 14:45
  • You can check the product is configurable or not. Use below to check product type: foreach( $orderItems as $item){ if ($item->getProductType()=="configurable") { foreach( $item->getChildrenItems() as $chitem){ // child product echo $chitem->getSku(); } } else { // simple product } } – Varun Parmar Feb 25 at 11:36
  • I discovered this method called getChildrenItems() is for Magento 1.9, not for Magento 2. Thanks anyway – Oscar Vaz Feb 25 at 16:09
  • Good. is it works now? – Varun Parmar Feb 26 at 6:41
0

At the end I could not get the attributes of the child items, but I solved the problem by creating the products again, and this time gave the attributes (height, width, length and weight) from the beginning to the parent products, because once you add configurable products this attributes gets disabled.

So I added the attributes first and then add the configurable products, by doing that I could simple use $product->getData("ts_dimensions_height");

Greetings!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.