addFieldToFilter not returning attribute values
-
16-10-2019 - |
Question
I have a product attribute called remote_image_link. When I retrieve a product using this code:
$mageProduct = Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('sku',"1234")->getFirstItem();
And then try and get the value of the attribute RemoteImageLink,
$mageProduct->getRemoteImageLink()
Nothing is returned. But when I add this code:
$mageProduct = Mage::getModel('catalog/product')->load($mageProduct->getId());
Then it works and I can get a value for the remote_image attribute.
How come I have to call load, aren't I loading using the addFieldToFileter
Solution
First of all use addAttributeToFilter
instead of addFieldToFilter
. It's cleaner that way.
And retrieve your product like this:
$mageProduct = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('remote_image_link')
->addAttributeToFilter('sku',"1234")->getFirstItem();
When getting a collection not all attributes are added to the collection. You need to add manually the attributes you need.
If for some reason that doesn't work, set the attribute remote_image_link
to be Used in product listing
.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange