0

I am trying to gather product attributes on the product detail page. We have the default attributes disabled through xml, and are trying to load them from within a custom phtml template file.

I am able to load certain data points, like weight with the following:

$_product = $block->getProduct();
echo $_product->getData('weight');

However, I would like to do this dynamically from the info loaded in the product page and loop through them, including the default attribute set and custom attributes. This info must be available because it's a product detail page.

I have tried

$attributes = $_product->getAttributes();
$customAttributes = $_product->getCustomAttributes();

but not everything is shown..

Can someone point me in the right direction? Magento documentation is terrible and so are many of the examples I've found. How come Magento can't be as simple as:

$_product = $block->getProduct();
$attributes = $_product->getAttributes();
foreach ($attributes as $attribute) {
    echo $attribute->getLabel();
    echo $attribute->getValue();
}

Can anyone assist? Thanks

  • $this->helper('Magento\Catalog\Helper\Output')->productAttribute($block->getProduct(), $block->getProduct()->getCustomAttribute(), 'custom_attribute') – Abhishek Panchal Nov 14 '18 at 19:55
  • Is that suppose to be helpful? – IamFace Nov 14 '18 at 21:47
  • Did you try it ? – Abhishek Panchal Nov 14 '18 at 21:47
  • What is your attribute code? – Abhishek Panchal Nov 14 '18 at 21:56
  • I'm trying to get ALL attributes. I would like to simply return an object of all attributes for a product; default, custom, attribute sets, etc – IamFace Nov 14 '18 at 21:59

Your Answer

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

Browse other questions tagged or ask your own question.