5

I got the attribute value 232 from the below code

    $_product->getData('farben');

How I get its label from this value?

marked as duplicate by Rakesh Jesadiya, 7ochem, Raphael at Digital Pianism, Amit Bera, Qaisar Satti Oct 19 '16 at 6:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

14

Please try this code

  1. First you have to load the products

    $_productCollection = $block->getLoadedProductCollection();

  2. In the product listing page ,there will be a foreach loop for listing the products like this

    foreach ($_productCollection as $_product)

  3. Your code will be inside this loop.Place the below code at a place where ever you want to display the attribute label.

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

  • It works for me. – Divya Muralidharan Oct 18 '16 at 12:36
  • 1
    getResource is deprecated in Magento 2.3.0 – Pathfinder Jan 8 at 8:01
  • @DivyaMuralidharan this code return attributes value not label – Hitesh Balpande Jan 11 at 14:24
5
 $_product = $productObject; //get product object

 $optionId = 232;   
 $attr = $_product->getResource()->getAttribute('attribute_code');
 if ($attr->usesSource()) {
    $optionText = $attr->getSource()->getOptionText($optionId);
 }
 echo $optionText;
  • how can we get custom attribute label in custom module block using phtml file,actually i am able to get custom attribute value. – Hitesh Balpande Jan 11 at 13:43
  • 1
    This run faster than accepted answer – Nolwennig Apr 14 at 23:39
-2

$product = $this->objectFactory->get('Magento\Catalog\Model\Product')->load($product_id);

$attributes = $product->getAttributes();

foreach ($attributes as $attribute) {
     label => __($attribute->getStoreLabel()),
     'value' => $value, //attribute Value
     'code' => $attribute->getAttributeCode(), //attribute Code
}

  • 10
    How is this accepted? It has errors and incorrect syntax. – Tisch May 11 '17 at 16:06

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