Skip to content

Facing issue in fetching child product attributes value

I have following code, In which I am fetching attribute values for child and parent products:

 public function setAttributeValues(array $products)
    {
        $this->values = [];
        foreach ($products as $product) {
            $attributes = $product->getAttributes();
            foreach ($attributes as $attribute) {
                $value = $attribute->getFrontend()->getValue($product);
                if (empty($value)) {
                    $value = $product->getData($attribute->getAttributeCode());
                }
                $this->setValue($attribute->getId(), $product->getId(), $value);
            }

            if ($product->getTypeID() == Configurable::TYPE_CODE) {
                $children = $product->getTypeInstance()->getUsedProducts($product);
                foreach ($children as $child) {
                    $childAttributes = $child->getAttributes();

                    foreach ($childAttributes as $childAttribute) {
                        $value = $childAttribute->getFrontend()->getValue($child);
                        print_r($value); // Facing attribute issue here
                        $this->setValue($childAttribute->getId(), $child->getId(), $value);
                    }
                }
            }
        }

I am getting null values for some attributes. In if ($product->getTypeID() == Configurable::TYPE_CODE) {} part. I’ve tried multiple logic to fetch attribute value but none of them worked for me. Also, I’ve verified that attribute value are set for the the particular product.

Any help would be really appreciated.