1

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

3

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.

  • so load will pull in everything but a collection won't pull in all attributes. That is confusing from a developers perspective. I would assume both actions would return the same result. – jstrez77 Dec 3 '13 at 15:28
  • I know it can be confusing at first, but there is a reason for that. Performance. When getting a collection you may not need all the attributes so there is no point in loading all. If you want to load all you can add ->addAttributeToSelect('*') – Marius Dec 3 '13 at 15:30

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.