1

I have created a custom product attribute using a module.

$eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'special_feature',
            [
                'type' => 'text',
                'label' => 'Special Feature',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 12,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'Special Feature',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            ]
        );

The attribute is showing in the product edit page and getting saved. Now, when I try to get the value using $product->getSpecialFeature() in the .phtml file, it is not showing anything. I am looping through the product collection generated from the code below in my Block class.

public function getProductCollectionByCategories($ids)
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addCategoriesFilter(['in' => $ids]);
        return $collection;
    }

2 Answers 2

0

To fetch value of attribute Use:

First get Your Product Collection and then

$this->getProduct()->getAttributeText('attribute_text');

e.g: $this->getProduct()->getAttributeText('special_feature');

or please use the productRepositry with get method by product id

2
  • What is $this? Where do I need to call this, in the collection loop?
    – Akhilesh
    Commented Dec 21, 2022 at 12:11
  • Please loop through the product collection and use the productRepositry with get method of product id . Commented Dec 21, 2022 at 12:19
0

Use below code

foreach ($collection as $product) {
    print_r($product->getAttributeText('special_feature'));     
    echo "<br>";
}

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.