2

On catalog page, I want to display custom attribute near each product (e.g. shoes_model). When I try to fetch it:

$_product->getAttributeText('shoes_model'); // returns nothing

... it returns empty value. But when I try to get another attribute, e.g. brand, it returns correct value:

$_product->getAttributeText('brand'); // returns brand name

Can anyone explain why is this happening and how to include custom attribute in catalog products collection.

p.s. Option "Used in Product Listing" is set to Yes for both attributes, and I'm 100% sure that shoes_model is defined.

0

4 Answers 4

7

Everything was simpler than I thought, as shoes_model is text-based attribute, I should get it like so:

$_product->getShoesModel();

or like so:

$_product->getData('shoes_model');
1

Follow this code

 $attribute = $_product->getResource()->getAttribute('custom_attribute_code');
    if ($attribute)
    {
        echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
    }
0

Try this:

<?php $_product = $this->getProduct(); ?>

<?php echo $_product->getData('attributename'); ?>

And also make sure you have included the attributes in layout, and that they are set to "visible on frontend".

0

Try using below code:

<?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product 
    $shoes = $product->getResource()->getAttribute('shoes_model')->getFrontend()->getValue($product); ?>

Now you can use this variable $shoes anywhere you like

Hope it will work for you.

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.