Update products programmatically in Magento

user971401 picture user971401 · Sep 29, 2011 · Viewed 58k times · Source

I'm working on a script that will create or update products in my catalog.
The script works fine when the product needs to be created, but it fails when the product already exists in the database giving me (many times) the following messages :

2011-09-30T08:00:53+00:00 ERR (3): Recoverable Error: Argument 3 passed to Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract::_canUpdateAttribute() must be an array, null given, called in ...
2011-09-30T08:00:53+00:00 ERR (3): Recoverable Error: Argument 3 passed to Mage_Eav_Model_Entity_Abstract::_canUpdateAttribute() must be an array, null given, called in ...
2011-09-30T08:00:53+00:00 ERR (3): Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in ...

I've been looking at the method quoted in the message, but I can't find any reason why the script fails.
The script first try to load a product using :

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

and then test if the product was retrieved using a simple if(!$product) { //creation }.
All the code that follow the if statement is shared for creation or update and consists of setter calls on product object.

Here is the code I use :

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if(!$product) {
    // the product doesn't exist yet
    $product = new Mage_Catalog_Model_Product();
    $product->setSku($sku);
    $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
    $product->setCreatedAt(strtotime('now'));
}
// setters calls
$product->setTeinte(trim((string)$record->web_teinte));
// ...
// finally save the product
$product->save();

Maybe someone has already faced the same problem.
Any help is welcome ! Thank you.

Answer

AMP picture AMP · Sep 30, 2011

Chances are, in your "setter calls" you are trying to set something that cannot be directly set on $product. It could even be the "setTeinte" as I am not sure what that is trying to set. But as we cannot see all your code, it is a little difficult to say, so as I guide, take a look at the code below, which sets some information directly on the product and then stock levels. It does therefore, illustrate how certain data has to be set. I hope it helps.

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

if ($product) {
    //Product found, so we need to update it in Magento.

    $product->setName((string)$XMLproduct->Name);
    $product->setPrice((real)$XMLproduct->SalePrice);
    //$product->setDescription((string)$XMLproduct->LongDescription);
    //$product->setShortDescription((string)$XMLproduct->Description);

    $product->save();

    $productId = $product->getId();
    $stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
    $stockItemId = $stockItem->getId();

    $stockItem->setData('manage_stock', 1);
    $stockItem->setData('qty', (integer)$XMLproduct->QtyInStock);

    $stockItem->save();

    echo $SKU," Updated: Name: '",(string)$XMLproduct->Name,"', Price: ",(real)$XMLproduct->SalePrice,", Stock level: ",$XMLproduct->QtyInStock,PHP_EOL;

    $updated++;
}