Reading Custom Attribute Values from Magento 2 Products

Reading Custom Attribute Values from Magento 2 Products

Custom attributes are where most store-specific data lives. Magento 2 can return both the raw stored value and the readable label.

Example

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($_item->getProduct()->getId());

<?php 
      $attr = $_product->getResource()->getAttribute('brand');
      if ($attr->usesSource()) {
        $optionText = $attr->getSource()->getOptionText($_product->getBrand()); ?>
      <div class="brand">
        <span><?php echo $optionText; ?></span>
      </div>
    <?php 	} ?>

Use getData() or the attribute getter for text values, and getAttributeText() for dropdowns, so you get “Blue” instead of an option ID.

Want this done for you? Find out more about our Magento development service.

Keep reading

Related articles

All articles

Need a hand?

Stuck on something similar?
We can fix it with you.

Send us the problem. A senior developer reads every message and replies within one working day.