0

I have a custom attribute called 'Color', which is a select box,

I have a instance of the product, and I want to get the selected value of my field 'Color'. The problem is when I try to do it its always returning null.

This is my code:

$product->getCustomAttribute('prj_color')

This is always null. But if I try to get a different field, which is not a multivalued field, a textbox for example, I can get the value normally.

I'm using Magento 2.1.5. Someone know whats going on?

5
  • did you save any product with color attribute?
    – Jaimin
    Jan 23, 2018 at 11:59
  • Yes I did. All products have the colors Jan 23, 2018 at 11:59
  • prj_color is your attribute id? Jan 23, 2018 at 12:00
  • Yes it is my attribute id Jan 23, 2018 at 12:01
  • you can call like, $_product->getPrjColor() Jan 23, 2018 at 12:07

2 Answers 2

1

get VAlue

<?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('c_address')->getFron‌​tend()->getValue($_p‌​roduct); ?>

LABEL

$address =$_product->getResource()->getAttribute('c_address')->getStoreLabel();

Check this file ::Magento/Catalog/templates/product/view/attribute.phtml

6
  • I've try to use this loop before, and the result of $attribute->getFrontend()->getValue($product) is empty string Jan 23, 2018 at 12:10
  • check my this post also magento.stackexchange.com/questions/210071/… Jan 23, 2018 at 12:11
  • ttribute Value <?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('c_address')->getFrontend()->getValue($_product); ?> Jan 23, 2018 at 12:13
  • Label ::$address =$_product->getResource()->getAttribute('c_address')->getStoreLabel(); Jan 23, 2018 at 12:13
  • 1
    the first solution worked for me
    – fudu
    Dec 7, 2018 at 1:55
0
<?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getPrjColor(), 'prj_color') ?>

Try this

4
  • BHow can I get this $_helper variable? Jan 23, 2018 at 12:03
  • is it working or not? because i have tried for getting my description for listing page so it may be work
    – Jaimin
    Jan 23, 2018 at 12:04
  • you just want to put this code where you want to get your product attribute
    – Jaimin
    Jan 23, 2018 at 12:10
  • Nope, it didn't work Jan 23, 2018 at 12:11

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.