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: 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: 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
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?