1

In Magento 2, I would like to access the value of custom attribute name material associated simple product from a configurable product.

At the moment, I am unable to get attribute value. For some reason it returns false but I can get other attribute values such a sku, price etc

Here is what I have tried to do so far:

../vendor/magento/module-catalog/view/frontend/templates/product/view/options.phtml

Then I have added this to get the simple products from the configurable products:

<?php $_product = $block->getProduct() ?>
<?php $_simpleProducts = $_product->getTypeInstance()->getUsedProducts($_product); ?>
<?php $attributeValues = explode(',', $_product->getResource()->getAttribute('material')->getFrontend()->getValue($_product)); ?>
<?php foreach ($_simpleProducts as $key => $simpleProduct): ?>
    <?php foreach ($attributeValues as $attributeValue): ?>
        <?php if ($attributeValue == $simpleProduct->getAttributeText('material')): ?>
            <strong><?php echo $attributeValue ?></strong>
        <?php endif; ?>
    <?php endforeach; ?>
<?php endforeach; ?>

I would like to do is set the material attribute for the configurable product then loop through the simple products to see if they have the same material but I can't get the value for material from the simple products.

Any ideas?

I can access it from the parent/configurable but when I try to get the value from the simple products it returns false enter image description here

2
  • Did you find a solution for this? I have the same problem and cannot find much useful in the web. Don't understand why this is not default behaviour in Magento2. Commented May 13, 2017 at 9:53
  • It isn't the default behaviour because of the EAV system. It means it can load a product without having to do a large join query for all the other stuff you may never read. It makes less sense if you are using the flat catalog but at least it is consistent. Commented Jun 29, 2017 at 10:47

2 Answers 2

1

you need to make your attribute visible.

For that you need to go to the Storefront Properties tab of your attribute, then change the Visible on Catalog Pages on Storefront to Yes.

Depending on your configuration you might need to reindex.

For information this allows magento to optimize the pages, if it had to load all attributes for all pages where a product is displayed it would be to slow.

1
  • 1
    The attribute is visible on the frontend but I can only seem to get the value from it for the parent/configurable product when I try to access the the same attribute value for the simple products it returns false Commented Jan 24, 2017 at 17:05
1

You are having this problem because when the product was loaded the code made it more efficient by only partially loading the data. This is much quicker when flat catalog is switched off since in that case each attribute is in a different table. I solve this problem by reloading the object.

First inject \Magento\Catalog\Model\ProductRepository $productRepository, into your block.

Then do this to reload the product, hopefully with all the attributes. It certainly has more attributes.

$product = $this->_productRepository->get($partialProduct->getSku());

I'll add the caveat that I'm not particularly enamoured with this method. I can't help but feel it would be more satisfactory to just populate the existing product.

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.