Why Can't I Load a Product by SKU using loadBySku()?Get productimage based on SKUHow can I get a product's status for a store?How do I get the minimal price of a grouped product from a product collection?How can I delete configurable product attributes?Code to enable downloadable product?Magento - $product->loadByAttribute('sku', $sku) not workingDynamically updating the sku on the product page of a configurable product based on selected optionsLoading non-existent productcatalog_product_collection_load_after causing maximum nesting issue for configurable productsHow to get all Products including disabled in Magento 1.3Getting all products of orders of last 24 hours in magento 1 in a large database
Quick destruction of a helium filled airship?
How do I answer an interview question about how to handle a hard deadline I won't be able to meet?
How to mock ApexTestQueueItem, AsyncApexJob, and ApexTestResult for test coverage?
What is the opposite of "hunger level"?
Why does Japan use the same type of AC power outlet as the US?
What's the point of writing that I know will never be used or read?
Good way to stop electrolyte tabs from turning into powder?
What's a good pattern to calculate a variable only when it is used the first time?
What if a restaurant suddenly cannot accept credit cards, and the customer has no cash?
How would armour (and combat) change if the fighter didn't need to actually wear it?
Is this bar slide trick shown on Cheers real or a visual effect?
Will some rockets really collapse under their own weight?
Attacking the Hydra
What is the purpose/function of this power inductor in parallel?
String routines
What is the fastest way to level past 95 in Diablo II?
Resource is refusing to do a handover before leaving
Heyawacky: Ace of Cups
What should we do with manuals from the 80s?
May the tower use the runway while an emergency aircraft is inbound?
Select elements of a list by comparing it to another list
Can I use my OWN published papers' images in my thesis without Copyright infringment
Why do we use low resistance cables to minimize power losses?
Why is the battery jumpered to a resistor in this schematic?
Why Can't I Load a Product by SKU using loadBySku()?
Get productimage based on SKUHow can I get a product's status for a store?How do I get the minimal price of a grouped product from a product collection?How can I delete configurable product attributes?Code to enable downloadable product?Magento - $product->loadByAttribute('sku', $sku) not workingDynamically updating the sku on the product page of a configurable product based on selected optionsLoading non-existent productcatalog_product_collection_load_after causing maximum nesting issue for configurable productsHow to get all Products including disabled in Magento 1.3Getting all products of orders of last 24 hours in magento 1 in a large database
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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));
magento-1.8 product catalog product-attribute
add a comment |
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));
magento-1.8 product catalog product-attribute
add a comment |
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));
magento-1.8 product catalog product-attribute
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));
magento-1.8 product catalog product-attribute
magento-1.8 product catalog product-attribute
edited Apr 17 '15 at 14:25
musicliftsme
5,4475 gold badges27 silver badges70 bronze badges
5,4475 gold badges27 silver badges70 bronze badges
asked Apr 15 '14 at 15:55
ashash
9967 gold badges16 silver badges39 bronze badges
9967 gold badges16 silver badges39 bronze badges
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
// 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 withloadByAttribute()
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
add a comment |
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: $time1ntime2: $time2ntime3: $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
add a comment |
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
add a comment |
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/
add a comment |
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));
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f18421%2fwhy-cant-i-load-a-product-by-sku-using-loadbysku%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
// 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 withloadByAttribute()
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
add a comment |
// 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 withloadByAttribute()
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
add a comment |
// 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.
// 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.
edited Jan 29 '17 at 2:46
answered Apr 15 '14 at 16:18
musicliftsmemusicliftsme
5,4475 gold badges27 silver badges70 bronze badges
5,4475 gold badges27 silver badges70 bronze badges
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 withloadByAttribute()
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
add a comment |
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 withloadByAttribute()
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
1
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
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
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
3
I understand it, I sometimes find Magento is backwards compared to patterns.
– ash
Apr 16 '14 at 14:11
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
$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
@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
add a comment |
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: $time1ntime2: $time2ntime3: $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
add a comment |
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: $time1ntime2: $time2ntime3: $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
add a comment |
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: $time1ntime2: $time2ntime3: $time3</pre>";
Test results (in seconds):
time1: 0.024642
time2: 0.079715
time3: 0.007891
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: $time1ntime2: $time2ntime3: $time3</pre>";
Test results (in seconds):
time1: 0.024642
time2: 0.079715
time3: 0.007891
answered Oct 23 '14 at 20:40
TedTed
3733 silver badges9 bronze badges
3733 silver badges9 bronze badges
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Sep 6 '16 at 17:42
BrianBrian
3011 silver badge5 bronze badges
3011 silver badge5 bronze badges
add a comment |
add a comment |
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/
add a comment |
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/
add a comment |
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/
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/
edited Nov 2 '17 at 8:46
Priyank
5,5624 gold badges24 silver badges56 bronze badges
5,5624 gold badges24 silver badges56 bronze badges
answered Nov 2 '17 at 8:40
Javed UsmaniJaved Usmani
1
1
add a comment |
add a comment |
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));
add a comment |
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));
add a comment |
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));
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));
answered Aug 5 at 13:07
ShadowbobShadowbob
3245 silver badges14 bronze badges
3245 silver badges14 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f18421%2fwhy-cant-i-load-a-product-by-sku-using-loadbysku%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown