Magento – Create Duplicate quote item instead of updating existing quote item for same product

event-observerquotesales-quote

Requirement–

I have created a custom Purchase Order module for my Magento Store, in which a same product may be supplied from many suppliers. You will be fully cleared from below

example-

(1) A product P1 has been supplied to my store from three supplier say S1, S2 and S3 with quantity 5,10,15 respectively.
Now I want to show SUM of all quantities i.e (30=5+10+15) to catalog inventory Stock. till Now there is no problem in doing this.

(2) Now When same product P1 is added to cart(Either frontend or backend) with qty 5 or less the there is no problem, order process will be Normal(as default). But if qty of same product P1 is added or updated greater than 5 (let's say 8) in quote then two rows(one row with qty 5 and second row with qty 3) should be created in sales_quote_item table corresponding to supplier S1 and S2, but in cart (i.e. visisble to user On frontend and Backend as well) there should be only one row having qty 8. Same as if product P1 is added or updated with qty lets say 20 then there should be three rows corresponding to each supplier having qty 5, 10 and 5 but user should see only one row having qty 20.

(3) If user place order of this product P1 with qty 20 then three rows should be there in sales_order_item table with their qty.

(4) In invoice that will be printed out also should have single row for that product P1, so customer can see only one row of that product, he doesnt need to know about suppliers.

(5) In credit memo there should be three rows so admin can decide himself, product of which supplier should be returned .

What I am trying Now–

I am following "fschmengler" approach. I am trying to modify quote items while they are being converted to Order. Now I am stuck at How can I modify price and totals according to changed qty in sales table and How can I add same product multi-times in sales table and then recalculate order totals. I am using event sales_convert_quote_item_to_order_item

Best Answer

To change what's getting added to the cart you need to observe an event before the quote item is created. This way you can choose to update the qty of an existing item, add one or more new items, or both. Unfortunateley there is no event between Mage_Checkout_CartController::addAction() and Mage_Sales_Model_Quote::addProduct(), so if you don't find a crazy workaround, you'll have to rewrite one of the methods in this control flow:

Add product to cart sequence diagram

I'd suggest Mage_Sales_Model_Quote::addProductAdvanced() because this is where addItem() is called. Also, manipulating the product options and merging alone will not work because of the case where you want to add several new items (7 => 5 + 2)

To create separate items for the same product, i.e. prevent that they get merged again, it's enough to add an invisible custom option to each item with a unique value.

Another possible approach would be to observe sales_quote_collect_totals_before which is called every time you visit the cart, and then rearrange the quote.

For each product that should be treated like this: - check for items with qty > 5 - reduce the qty of those items to 5 - if there are items with qty < 5, add remaining qty up to 5 - add new items for the remaining qty (again, with a unique custom option)

Related Topic