2

I got the attribute value 232 from the below code

$_product->getData('farben');

How I get its label from this value?

4 Answers 4

9

Try with

$_product->getAttributeText('farben');
5

Please try this code

Step 1)First you have to load the products

$_productCollection = $block->getLoadedProductCollection();

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

foreach ($_productCollection as $_product)

Step3)Your code will be inside this loop.Place the below code at a place whereever you want to display the attribute label.

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

Just replace your_attribute_code with whatever your attribute is named.

2
  • @Divya actually this code is used to get custom attribute values not label.If you have any idea please give related answer Commented Jan 11, 2019 at 14:07
  • $_product->getResource() its a deprecated functionality and should not be used
    – Nuzil
    Commented May 20, 2022 at 10:04
2

Try this

<?php $_product = $block->getProduct(); ?>
<?php if (null !== $_product->getCustomAttribute('attribute_code')): ?>
    <?= $_product->getAttributeText('attribute_code'); ?>
<?php endif; ?>
1
  • You should maybe explain the code, so OP understands the problem.
    – user11753067
    Commented Jul 26, 2019 at 12:22
1
$passing_marks = $_product->getResource()->getAttribute('attribute_code');

$passing_marks->getFrontend()->getLabel($_product);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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