2

At admin backend, save product was giving SQL errors like :

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1161-1' for key 'UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID', query was: INSERT INTO cataloginventory_stock_item (product_id, stock_id, qty, use_config_min_qty, is_qty_decimal, use_config_backorders, use_config_min_sale_qty, use_config_max_sale_qty, is_in_stock, low_stock_date, use_config_notify_stock_qty, use_config_manage_stock, stock_status_changed_auto, use_config_qty_increments, use_config_enable_qty_inc, is_decimal_divided) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, '2016-01-06 15:56:50', ?, ?, ?, ?, ?, ?)

Error cames every first attempt to save, after giving error (SQL above) it gets saved in the second attempt, means I had to enter all the product detail again and second time it works.

It was fixed by changing $product->save() to $product->getResource()->save($product)

Is it the right way to replace $product->save()?

but now it does not updates the stock detail [Quantity / in Stock], rest I can add new product, everything works but not the stock options.

Any hint will be helpful.

0

Not a good method for every situation. Missing some data when product create. In my case missing to update the associated products when create the configurable product

0

There is a huge difference between $product->save() and $product->getResource()->save().

If you check app/code/core/Mage/Core/Model/Abstract.php, you will find the save() method. What this does is also execute the beforeSave and afterSave methods of the object. A lot of other extensions (both core and third party) "link" to these methods. So a lot of code will never be executed when you change this.

Your actual problem is in your database. The code tries to insert a new row with the product's stock levels, but fails because an entry with the product ID and stock item ID already exists. So that's the thing you need to fix. With your "fix", the stock levels will never be updated anymore.

If you go to the database and check the table cataloginventory_stock_item, you should see a row with the product_id "1161" and stock_id "1".

I'd suggest to first check if you have any extensions installed that alters the product save or stock item save. If so, you might want to disable this and check if the issue is solved. If that doesn't work out, I would debug the process within Magento itself.

As a last resort you could remove the row from the cataloginventory_stock_item table, but that would mean that the entire stock item is deleted and you have to enter all the stuff again manually in Magento and save the product. Also, if this happens for all products, you will have a bigger problem.

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.