Question

I'm creating products programatically on Magento CE 1.8.1. The form works fine except every once in a while we get a nasty error that comes and goes away, and does not affect all users.

:"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '970-1' 
for key 'UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID'"

Like I said, it doesn't always show up. I found many questions about this problem and saw as many suggestions to use

$product->getResource()->save($product);
// instead of 
// $product->save();

I tried that and I think the error does stop showing up, but a new problem arises; stock isn't saved properly. The code that I was using was:

// this works with $newProduct->save()
// but it doesn't work with $product->getResource()->save($newProduct)

$newProduct->setStockData(array(
              'qty'=>$params["qty"],
              'is_in_stock'=>1, 'notify_stock_qty'=>$params["stockqty"]));

I then tried using this:

$newProduct = Mage::getModel('catalog/product')->load($product_id);
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($newProduct);
$stockItem->setData('manage_stock', 1);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('use_config_notify_stock_qty', 0);
$stockItem->setData('qty', 40);
$newProduct->setStockItem($stockItem);
//save product..

But nothing. Stock is always 0, and manage_stock is always set to No. So I tried loading and saving the stock item after saving the product but I got a new error: "SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`bitnami_magento`.`cataloginventory_stock_item`, CONSTRAINT `FK_CATINV_STOCK_ITEM_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON D)" which sounds like the product hasn't been created yet! I checked the backend and the product was created, but with no stock. It seems like the resource save method ignores the stockData and also the stockItem.

I'm running out of ideas here. I've read a bunch of questions here where these worked but they're not working for me. Has anyone run into this before?

Was it helpful?

Solution

In your example where you load the Mage_CatalogInventory_Model_Stock_Item instance, you should save the instance itself instead of the product.

Saving the product doesn't work, because it ignores the assigned stock item. You can see why this is by looking at the the method

Mage_CatalogInventory_Model_Observer::saveInventoryData

This method is triggered by the catalog_product_save_after event, which is called after a product is saved. The method first checks whether stock_data is set; if it is not set, the stock item is not updated.

OTHER TIPS

This is a working example wich i use:

$product = Mage::getModel('catalog/product');
                $product->setWebsiteIds(array(1));
                $product->setTypeId('simple');
                $product->setName('name');
                $product->setDescription('xyz');
                $product->setShortDescription(' ');
                $product->setStatus(1);
                $product->setTaxClassId(1);
                $product->setWeight(0);
                $product->setCreatedAt(strtotime('now'));
                $product->setManufacturer(1);
                $product->setAttributeSetId(1);
                $product->setSku('sku');
                $product->setPrice(5);
                $product->setVisibility(1);
                $product->save();

After saving the product create stock item:

$stockItem = Mage::getModel('cataloginventory/stock_item');
            $stockItem->assignProduct($product);
            $stockItem->setData('is_in_stock', 1);
            $stockItem->setData('stock_id', 1);
            $stockItem->setData('store_id', 1);
            $stockItem->setData('manage_stock', 1);
            $stockItem->setData('use_config_manage_stock', 0);
            $stockItem->setData('min_sale_qty', 1);
            $stockItem->setData('use_config_min_sale_qty', 0);
            $stockItem->setData('max_sale_qty', 1000);
            $stockItem->setData('use_config_max_sale_qty', 0);
            $stockItem->setData('qty', 10);
            $stockItem->save();

Before saving the new stock data, try loading the stock item from the product, and deleting it.

$stockItem = Mage::getModel('cataloginventory/stock_item')
    ->loadByProduct($product)
    ->delete();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top