18

How to get attribute of product?

7 Answers 7

24

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();
}
0
24

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.

1
  • 3
    Like your solution, changed <referenceBlock to <referenceContainer and it worked as "product.info.main" is a container :)
    – Devtype
    Commented Nov 24, 2016 at 11:08
13

I had solution for my issue:

$product = $this->productRepository->getById($product);
$attr = $product->getData('status');
7
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
$_product->getData('attr_code');

Hope it helps

1
  • 3
    Please, 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.
    – Dynomite
    Commented Feb 20, 2017 at 20:52
5

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

2
  • this is a better way to do it than using object manager which is almost always discouraged. +1
    – Dynomite
    Commented Feb 20, 2017 at 20:49
  • best solution that I found. +1 :D
    – jehzlau
    Commented Sep 29, 2017 at 5:48
1

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>
0

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

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.