How to get attribute of product?


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="-">
            <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>

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.

  • 3
    Like your solution, changed <referenceBlock to <referenceContainer and it worked as "product.info.main" is a container :) – Devtype Nov 24 '16 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);

Hope it helps

  • 2
    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 Feb 20 '17 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 – Dynomite Feb 20 '17 at 20:49
  • best solution that I found. +1 :D – jehzlau Sep 29 '17 at 5:48

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="-">
        <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>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.