Magento Forum

   
get product by sku
 
d.giuralarocca
Jr. Member
 
Total Posts:  8
Joined:  2008-09-03
 

Hi,
i need to retrieve a product by sku. i tried this suggested here
but it did not work.
here is the code.

$product Mage::getModel('catalog/product');
            
            
// get option id for Black value of color attribute in current public release
            
$source $product->getResource()->getAttribute('sku')->getSource();
            
$optionId Mage::getSingleton('catalog/config')->getSourceOptionId($source'fiorentini002');
        
                
            
// load a product collection filtered by color=black
            
$productCollection $product->getCollection()
                ->
addAttributeToSelect('*')
                ->
addAttributeToFilter('sku'$optionId)
                ->
load();
            
            
// process the collection
            
foreach ($productCollection as $prod{
                Mage
::log($prod->getSku());
            
}

some help?

 
Magento Community Magento Community
Magento Community
Magento Community
 
joyously
Guru
 
Total Posts:  447
Joined:  2008-08-21
 

If you have a product object, like in your first line, you can use something like what is done in the import:

$productId $product->getIdBySku($importData['sku']);
  if (
$productId{
      $product
->load($productId);
  
}
It requires the exact SKU and it only gets one product (because SKUs have to be unique). I made a local modification so I can lookup SKUs using LIKE instead of =, but I’m getting a list of IDs, not entire products as a collection.

 
Magento Community Magento Community
Magento Community
Magento Community
 
anthonyfojas
Member
 
Total Posts:  33
Joined:  2008-08-19
 

You can use the loadByAttribute function.

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

 
Magento Community Magento Community
Magento Community
Magento Community
 
FishPig
Sr. Member
 
Avatar
Total Posts:  232
Joined:  2009-06-05
 

You can use the load method. This is quicker and easier than all other solutions.

<?php

$_product 
Mage::getModel('catalog/product')->load('product-01-sku''sku');

For more information on loading products click here.

Source: Load categories or products by an attribute (sku, name etc) in Magento.

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagePsycho
Mentor
 
Avatar
Total Posts:  1702
Joined:  2009-06-23
 

$_product Mage::getModel('catalog/product')->loadBySku('product-01-sku');
don’t know whether this works or not.

But AFAIK:

$_product Mage::getModel('catalog/product');
$prodcut->load($_product->getIdBySku('product-01-sku')); 

// or

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

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  3215
Joined:  2009-02-26
Minneapolis MN
 

I get this error in 1.4 when I tried the loadBySKU

$product Mage::getModel('catalog/product')->loadBySku($sku);

Invalid method Mage_Catalog_Model_Product::loadBySku(Array

But this works fine:

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

 
Magento Community Magento Community
Magento Community
Magento Community
 
bravehartk2
Member
 
Avatar
Total Posts:  34
Joined:  2008-08-14
 

loadBySku is not a Product-Model-Function anymore so use loadByAttribute is the right way to do the job.

I now this thread is a dead body but i think there has to be a clear answer grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
chiefair
Mentor
 
Avatar
Total Posts:  1839
Joined:  2009-06-04
 

Or per FishPig, SKU is a static attribute in the main product entity table (catalog_product_entity) and this gets it faster

$_product Mage::getModel('catalog/product')->load($sku'sku');

 
Magento Community Magento Community
Magento Community
Magento Community
 
markoshust
Sr. Member
 
Avatar
Total Posts:  129
Joined:  2007-09-05
Cleveland, OH
 

Depending on what you’re doing, sometimes that code snippet doesn’t load all of the data that is needed. In this case, this returns all data properly:

$product Mage::helper('catalog/product')->getProduct('SKU_GOES_HERE'Mage::app()->getStore()->getId(), 'sku');

 
Magento Community Magento Community
Magento Community
Magento Community
 
kiatng
Enthusiast
 
Total Posts:  875
Joined:  2008-09-03
Kuala Lumpur, Malaysia
 
markoshust - 08 November 2012 05:09 PM

Depending on what you’re doing, sometimes that code snippet doesn’t load all of the data that is needed. In this case, this returns all data properly:

$product Mage::helper('catalog/product')->getProduct('SKU_GOES_HERE'Mage::app()->getStore()->getId(), 'sku');

This is the only method that works for me in ver 1.7.0.2.  The load method produces a foreach error in line 954 in Mage_Eav_Model_Entity_Abstract, and the loadByAttribute method, which is resource intensive, does not return any result in a shared hosting environment.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rjhartman
Jr. Member
 
Total Posts:  2
Joined:  2010-11-17
 
markoshust - 08 November 2012 05:09 PM

Depending on what you’re doing, sometimes that code snippet doesn’t load all of the data that is needed. In this case, this returns all data properly:

$product Mage::helper('catalog/product')->getProduct('SKU_GOES_HERE'Mage::app()->getStore()->getId(), 'sku');

Thanks Markoshust.  Your solution worked for me in 1.7 after an upgrade from 1.2.  Other methods are not building the stock_item object for the product and just returning a blank varien object instead.  I think Magento have changed the way their methods work in later versions.

Richard

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top