Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

*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()
    ->addAttributeToSelect($additionalAttributes)
    ->addAttributeToFilter($attribute, $value)
    ->setPage(1,1);

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

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');

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')
                                            ->getFirstItem();

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top