For Loading product by sku i'm using following function. For some skus its working fine where as for some sku's not working.And these sku products existed in back end.

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

Can you help anyone?


If neither of the following are working.

  1. Mage::getModel('catalog/product')->load($sku, 'sku');
  2. Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

Then you could always use the resource model, Mage_Catalog_Model_Resource_Product, to get the product id via the function getIdBySku and then simply do a normal load.

| improve this answer | |
  • Unfortunately no solution is working. – sivakumar Oct 31 '14 at 6:22

*loadByAttribute() basically load the product collection and filter those collection by sku and then it return that collection first item as a result.

That particular sku does not exits at result because of that particular product' does not exits to the products collection

$collection = $this->getResourceCollection()
    ->addAttributeToFilter($attribute, $value)

As per as,magento a product does not visible at collection because of Product's flat setting enable and due to some reason like magento condition like visibility, stock, website,store ,a product not include at product flat collection.

If you load the product by load by Mage::getModel('catalog/product')->load($id); And this code is direct called of product model that you can get result and also it does not called from collection,just call a particular model by id that

| improve this answer | |

Since sku attribute exists in catalog_product_entity table itself you can load product by the field like this:

$_product = Mage::getModel('catalog/product')->load($sku, 'sku');
| improve this answer | |

This only works when flat tables are turned off. The best solution is to use collection to fetch the product - it will always work.

$product = Mage::getModel('catalog/product')->getCollection()
                                            ->addAttributeToFilter('sku', 'test-sku')
| improve this answer | |

Disable the "use flat tables" config or reindex the product data

| 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.