2

I have a custom template where I would like to get custom product attribute value, in my particular case is a yes/no attribute type. This has troubled me for quite a while as I can get all default attribute value, such as sku, id, price etc, but not my custom attribute value. The code I used was:

$attribute_value = $_product->getResource()->getAttribute('custom_attribute_code')->getFrontend()->getValue($_product);

It will always return Yes no matter what.

Until I gave 'objectManager' a go and it worked and returned the correct attribute value and the code I used are:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
$attribute_value = $product->getData('custom_attribute_code');

So my question is

1) Why the first option returns only "Yes" and is it a bug?

2) Can someone please explain the difference between the two?

Thanks!

  • You can use $_product->getAttributeText('custom_attribute_code') – Ankit Shah Mar 9 '17 at 6:40

Your Answer

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

Browse other questions tagged or ask your own question.