Magento – Magento 2 custom attribute value returns “No”

attributescustom-attributesmagento2

I'm trying to get custom attribute value by using:

$_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product);

however I keep getting "No" as actual value, no matter which custom attribute I'm trying to get, it all returns "No", include both simple and configurable products.

I also tried to get the label and it returns the correct label:
$_product->getResource()->getAttribute('attribute_code')->getFrontend()->getLabel()

What have I done wrong?

Best Answer

Hey please use below code :

$_product->getResource()->getAttribute('attribute_code')->setStoreId($currentStoreId)->getFrontend()->getValue($_product);

In above line i have just added ->setStoreId($currentStoreId) .