0

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.

2 Answers 2

0

@smit kandiya

Hey,

You can use a collection to fetch the attributes of child products

use

Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

protected $productCollectionFactory;

public function __construct(
    CollectionFactory $productCollectionFactory
) {
    $this->productCollectionFactory = $productCollectionFactory;
}

public function getChildProductAttributes($configurableProduct)
{
    $childIds = $configurableProduct->getTypeInstance()->getChildrenIds($configurableProduct->getId());
    $childIds = array_shift($childIds);

    $collection = $this->productCollectionFactory->create()
        ->addAttributeToSelect('attribute_code')
        ->addFieldToFilter('entity_id', ['in' => $childIds]);

    foreach ($collection as $childProduct) {
        $attributeValue = $childProduct->getData('attribute_code');
        // Process attribute value
    }
}

Thank You!

1
  • Hey, Thank you for the answer. Commented Jul 1 at 4:04
0

your issue is the product attribute configuration Those whose values is not printing you have to go to product attributes open the attribute and select YES in "Used in Product Listing"

Please don't forget to clear cache after changing value of attribute That will solve your issue

enter image description here

1
  • Hey, thank you for the answer. Commented Jul 1 at 4:05

Your Answer

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

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