Magento 2 - How to get attribute of product?

How to get attribute of product?

Solutions

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.

I had solution for my issue:

$product = $this->productRepository->getById($product);
$attr = $product->getData('status');

Similar questions

Get product attribute by attribute group code in magento 2
Please help. I want get attribute by [attribute_group_code]. Please help me. My code is as follows: I just want to get these attribute groups (operate) values based on ['attribute_group_code']. Because when creating the attribute group in admin, I will not know the id of the attribute group, so I only rely on the attribute_group_code. Thank you.
How to Get product attribute set id by attribute set name in Magento 2?
I have a product attribute set name DSLR, in frontend page i need to get product collection filtered b this product attribute set how can i get this product attribute set id by product attribute set name?
How to get Product Attribute Options by attribute code in Magento 2.0
I am trying to retrieve the list of dropdown attributes and check if the value exists (if it does i need to get the value and assign it to the product) and if it doesnt i will have to create it and get its value to assign it to the product.
Get attribute label by attribute code without load by product id
Is it possible to get the attribute label (depending on the storeview) only by the attribute code? I don't want to enter any product or product id. This is my code to get the attribute label: I want to get rid of the $_product.
How to get a product value attribute from attribute id?
I need to retrieve product attribute value based on the attribute id, here's how I get the id and label for each attribute
Get product collection based on custom attribute (drop down attribute value)
I created a custom drop down attribute in Magento2 from admin. I tried to collect all associated products by using filters. I am able to filter product collection based on text field attributes, for example But when I try to filter using dropdown custom attribute value I'm getting an empty array How am I supposed to filter product collection using ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.