Why Can't I Load a Product by SKU using loadBySku()?

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

Solutions

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

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

Similar questions

Why SKU's need to be unique for each product, don't we have product id for that?
Question : How product nos/item number/part number different from SKU? From what I understand, SKU groups all the attributes that applies to a product and assigns a fixed number/value to it so that that one number could define a set of values within an attribute group. For example if you were to make a stock transaction, lets say item is - Mens V n...
Sku
Why does my local.xml load javascript repeatedly using addItem
I am attempting to load a javascript file in my custom skin using the following code in local.xml. Why does it first attempt to look in the base theme skin directory, then my directory? I see it's attempting to load The file exists in /var/www/skin/frontend/NKI/NKI/js/nki.js I am a little confused. Maybe my method for loading using local.xml is out...
load multiple attributers using load() Magento
I am using this code but it's not working with multiple parameter. I want only use load() function in magento. You can see below code any please help me. Thanks for support in advance.
Magento 2 How to load product collection, Category collection, Load product by id and get category by product id
I do have experience with magento 1 and in magento 1 i do know how to load the product collection, category collection, quote and product load by id. Magento 2 is really new concept for me and syntax is completely changed in it. Need help how can i load Product collection, category collection, quote and product by id using magento 2 standard method...
Magento - 1 x SKU covering multiple SKU's - How to?
I'm struggling a bit on how to sell a product that is sold individually, but also in a package including more products. Let me illustrate it as a real example: Sample pack e-liquid (including 1x orange, 1x strawberry, 1x apple) Customer can choose: strength: 0, 6, 12, 18, 24 (meaning that each flavor has 5 SKU's) I can't figure out how to make this...
Shopping cart price rule SKU-A and SKU-B condition
How can I set a condition if I require two unique SKUs to be purchased first, before a discount is availed? For example, if SKU 0001 and SKU 0002 exists on the cart, then you'll get 50% OFF. Both SKUs are required. I already tried the if SKU "contains" SKU002,SKU001 condition, and the if SKU "is" SKU002,SKU001 condition, but it doesn't seem to sati...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.