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