27

Synopsis

I wanted to load a product by SKU there are many articles, blog posts, stack overflow results etc. All I want to know is - why does it have to be so difficult to load a product by a SKU?

// This method does not work (Of all, I expect this to work)
$product = Mage::getModel('catalog/product')->loadBySku($sku);

// These do not work either
$product = Mage::getModel('catalog/product')->loadByAttribute($sku, 'sku');
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

// This does not work
$product = Mage::getModel('catalog/product')->load($sku, 'sku');

Of course, I am expecting far too much from Magento at this point a simple way of doing something (simple is a concept Magento clearly is not familiar with)

// This works:
$product = Mage::getModel('catalog/product');
$product->load($product->getIdBySku($sku));
56
// This method does not work (Of all, I expect this to work)
$product = Mage::getModel('catalog/product')->loadBySku($sku);

This is because the method Mage_Catalog_Model_Product::loadBySku does not exist. There must be a method called loadBySku in the class Mage_Catalog_Model_Product for you to be able to use it, unless it's some magic method.

// These do not work either
$product->getModel('catalog/product')->loadByAttribute($sku, 'sku');
$product->getModel('catalog/product')->loadByAttribute('sku', $sku);

The above includes typos. It should be like below. You need to assign what Mage::getModel()->load() returns to $product before doing stuff with the product object.

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

Note that with loadByAttribute, you do not get related objects like the stock and media gallery. See Mage_Catalog_Model_Abstract::loadByAttribute for more info.

  • 1
    Typos have been corrected ;) I'm only trying to understand why basic features in Magento are so complicated. If it's to do with architecture changes it would be useful to learn about these. The amount of time wasted trying to get Small and simple functional changes complete is stupid - I get reusable modules is the way, but sometimes not everything can be reusable. – ash Apr 15 '14 at 19:14
  • Do you understand how the product loading work, though? I felt the same way at first, but it gets better. – musicliftsme Apr 15 '14 at 19:44
  • 3
    I understand it, I sometimes find Magento is backwards compared to patterns. – ash Apr 16 '14 at 14:11
  • $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku); it`s works for me :) thanks!!! – jruzafa Feb 24 '15 at 17:31
  • @jruzafa, note with loadByAttribute() that you don't get all of related product data. It will be missing things like the stock and media objects. – musicliftsme Feb 24 '15 at 19:12
21

I tested execution time for the various methods I've seen to load a product by SKU. This is the most efficient:

$product = Mage::getModel('catalog/product');
$product->load($product->getIdBySku($data['sku']));

Tests:

$time_start = microtime();
$product1 = Mage::getModel('catalog/product')->loadByAttribute('sku', $data['sku']);
$time_end = microtime();
$time1 = $time_end - $time_start;

$time_start = microtime();
$product2 = Mage::getSingleton('catalog/product')->getCollection()
            ->addAttributeToFilter('sku', $data['sku'])
            ->addAttributeToSelect('*')
            ->getFirstItem();
        // do a full product load, otherwise you might get some errors related to stock item
        $product2->load($product2->getId());
$time_end = microtime();
$time2 = $time_end - $time_start;

$time_start = microtime();
$product3 = Mage::getModel('catalog/product');
$product3->load($product3->getIdBySku($data['sku']));
$time_end = microtime();
$time3 = $time_end - $time_start;

echo "<pre>time1: $time1\ntime2: $time2\ntime3: $time3</pre>";

Test results (in seconds):

time1: 0.024642
time2: 0.079715
time3: 0.007891
  • this benchmark is insufficient, possible variation in just a single run are to high. Also the cache inside the DB could have a big impact on the results here – Flyingmana Sep 6 '17 at 9:38
1

To load product by SKU in magento you can use the following code:

$_sku = 'leathershoes';
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$_sku);
print $_product->getName(); // display product name

I used this everytime and it works perfectly. If you want to load multiple products, try this

$productSku = array('234', '267', '4523', 'Leather shoes', 'skin care'); // insert product SKU here
$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
$collection = Mage::getModel('catalog/product')
                ->getCollection()                
                ->addAttributeToFilter('sku', array('in' => $productSku))
                ->addAttributeToSelect($attributes);

Reference source http://magentoexplorer.com/how-load-product-by-sku-or-id-in-magento

0

As Already Discuss, This is because the method Mage_Catalog_Model_Product::loadBySku does not exist. There must be a method called loadBySku in the class Mage_Catalog_Model_Product for you to be able to use it.

You can also use loadByAttribute() methods to get the products details by sku.

    $sku  =  $this->getRequest()->getParam('SKU');
    $catalogMageObj = Mage::getModel('catalog/product');
    $products = $catalogMageObj->loadByAttribute('sku', $sku);

    // get the products details which you want
    $data = array(
    'id' => $products->getEntityId(),
    'sku' => $products->getSku(),
    'name' => $products->getName(),
    'attribute_set_id' => (int)$products->getAttributeSetId(),
    'price' => $products->getPrice(),
    'status' => $products->getStatus(),
    'visibility' => $products->getVisibility(),
    'type_id' => $products->getTypeId(),
    'created_at' => $products->getCreatedAt(),
    'updated_at' => $products->getUpdatedAt(),
    'product_links' => $productstypes, 
    'custom_attributes'  => $custom_attributes
);

// return the response as array
return $data;

You May also have a look on this article for more details. https://www.ipragmatech.com/ultimate-magento-developer-guide-get-product-details-using-restapi-magento-1-x/

0

Beware of $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);!
It may work 99% of the cases, but this will result in loading the attribute stock_item as Varien_Object instead of Mage_CatalogInventory_Model_Stock_Item so on methods like Mage_CatalogInventory_Model_Observer::checkQuoteItemQty it will call $stockItem instanceof Mage_CatalogInventory_Model_Stock_Item which would throw an exception!

So the best way is:

$product = Mage::getModel('catalog/product');
$product->setStoreId($storeId)->load($product->getIdBySku($sku));

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.