Hello I have created one dropdown attribute with code "custom_collection"

I want to show it on product listing page under each product so in list.phtml file i added this code

$attr_m = $_product->getData('custom_collection');

echo $attr_m;

It returns some id but its not showing the attribute value


I did not find anywhere so i tried custom query and it works

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

 $collection_attr_id = $_product->getData('custom_collection');

    $this->_resources = \Magento\Framework\App\ObjectManager::getInstance()
    $connection= $this->_resources->getConnection();
    $themeTable = $this->_resources->getTableName('eav_attribute_option_value');
    $sql = "Select value FROM " . $themeTable . " WHERE option_id = " . $collection_attr_id . ";";
    $result = $connection->fetchAll($sql); 
    echo $result[0]['value'];

| improve this answer | |

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.