8

when trying to load a product,

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

we get the following error:

Fatal error:
Uncaught exception 'Exception' with message 'Warning: Invalid argument supplied for foreach()
in app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 954' in app/code/core/Mage/Core/functions.php:245 Stack trace:
#0 app/code/core/Mage/Eav/Model/Entity/Abstract.php(954): mageCoreErrorHandler(2, 'Invalid argumen...', '/var/www/magent...', 954, Array)
#1 app/code/core/Mage/Catalog/Model/Resource/Abstract.php(698): Mage_Eav_Model_Entity_Abstract->load(Object(Mage_Catalog_Model_Product), '120x180 ES-bale...', 'sku')
#2 app/code/core/Mage/Core/Model/Abstract.php(225): Mage_Catalog_Model_Resource_Abstract->load(Object(Mage_Catalog_Model_Product), '120x180 ES-bale...', 'sku')
#3 updatemagento.php(197): Mage_Core_Model_Abstract->load('120x180 ES-bale...', 'sku')
#4 {main} thrown in app/code/core/Mage/Core/functions.php on line 245

Any ideas what's going wrong?

0

3 Answers 3

21

One option available to you would be to find the ID from the sku, and then load by ID.

$product = Mage::getModel('catalog/product');
$id = Mage::getModel('catalog/product')->getResource()->getIdBySku('your sku');
if ($id) {
    $product->load($id);
}
0
7

As mentioned by others, the Mage_Catalog_Model_Product::load method takes the ID of the product, not the SKU. The second argument of this method can be specified to define which attributes should be loaded into the model. It is not intended to be used as a "load by this attribute" field.

In order to load a product by an attribute, you should use:

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
1
  • 4
    Warning: loadByAttribute uses a collection and filters by the given attribute. This means, the observers for product loading don't apply and you don't have the complete data with stock, gallery etc. like you would have with load(). Jan 27, 2015 at 12:40
-2

$product= Mage::getModel('catalog/product')->load(123456); is the right method, where 123456 is the id of the product.

You could try that $product= Mage::getModel('catalog/product')->load('sku', 'mysku'); instead, I think the order of your argument was wrong.

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.