How to get attribute of product?
7 Answers
Another way, for the custom attributes: we can simply get the value by using getCustomAttribute()
if (null !== $product->getCustomAttribute('your_custom_attribute')) {
echo $product->getCustomAttribute('your_custom_attribute')->getValue();
}
Best practice in magento is to do it via xml.
To get a standard attribute you do something like this in catalog_product_view.xml
for example:
<referenceContainer name="product.info.main">
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.brand" template="product/view/attribute.phtml" before="-">
<arguments>
<argument name="at_call" xsi:type="string">getBrand</argument>
<argument name="at_code" xsi:type="string">brand</argument>
<argument name="css_class" xsi:type="string">brand</argument>
<argument name="at_label" xsi:type="string">none</argument>
<argument name="add_attribute" xsi:type="string">itemprop="brand"</argument>
</arguments>
</block>
</referenceContainer>
This will get the value of an input attribute or textarea. If you have a dropdown you should use the text type, so add this line in the list of arguments:
<argument name="at_type" xsi:type="string">text</argument>
No need to create files or write any php code to get an attribute. This way you'll use the same default php code for any attribute and you'll have to change it only once if needed.
-
3Like your solution, changed <referenceBlock to <referenceContainer and it worked as "product.info.main" is a container :)– DevtypeCommented Nov 24, 2016 at 11:08
I had solution for my issue:
$product = $this->productRepository->getById($product);
$attr = $product->getData('status');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
$_product->getData('attr_code');
Hope it helps
-
3Please, try to use a block class such as "Magento\Catalog\Block\Product\View\Description" but I would recommend not to use Object Manager in Magento 2 unless as a last resort.– DynomiteCommented Feb 20, 2017 at 20:52
Another way in phtml-files:
echo $this->helper('Magento\Catalog\Helper\Output')->productAttribute($block->getProduct(), $block->getProduct()->getDescription(), 'description')
as in: vendor/magento/module-catalog/view/frontend/templates/product/view/description.phtml
-
this is a better way to do it than using object manager which is almost always discouraged. +1– DynomiteCommented Feb 20, 2017 at 20:49
-
Creating a Block inside the catalog_product_view.xml and add inside any container you want or create a container around it.
<!-- Get a attribute -->
<block class="Magento\Catalog\Block\Product\View\Description" name="product.attributes.Height" template="product/view/attribute.phtml" before="-">
<arguments>
<argument name="at_call" xsi:type="string">getHeight</argument>
<argument name="at_code" xsi:type="string">height</argument>
<argument name="css_class" xsi:type="string">height</argument>
<argument name="at_label" xsi:type="string">none</argument>
<argument name="add_attribute" xsi:type="string">itemprop="Height"</argument>
</arguments>
</block>
I know it's been a while since the original post, but how would I modify this method so I can pull a dynamic attribute set rather than specifying it in the catalog_product_view.xml file - Block name="product.attribute.key-features" ? The project I'm working on has many possible attribute sets all for the same Key Features section of the product page. So one product may use attribute set A for it's key features and another product may use attribute set B for it's key features. Andy Help appreciated. Andy