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


(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

  • Which version of Magento are you using?
    – awavi
    Commented Mar 14, 2016 at 22:56

2 Answers 2


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)

  • Hello fschmengler, thanks for your detailed answer but please check my updated question. Commented Mar 5, 2016 at 6:27
  • If you only need the separation in the backend and the user must not see it, you should not alter the quote at all and modify the order items instead, when the quote is converted to an order. Commented Mar 5, 2016 at 8:43
  • I have no idea how can I do it what have you said, can you add some code snippet if you have a chance. Commented Mar 7, 2016 at 20:06
  • Why should the order totals change? Commented Mar 9, 2016 at 13:07
  • yeah fschmengler, I just got wrong thought, sorry for my comment, just deleted it. But how can I duplicate an item to order while converting from quote.? Like product p1 (qty-5) in quote should be in two rows with qty(3) and qty(2) Commented Mar 9, 2016 at 13:26

Method representProduct inside class Mage_Sales_Model_Quote_Item is responsible for comparing 2 items in quote and their merging behavior in one. You can create some rewrite for the method to check if the qty in cart is as you require. This should be straightforward to achieve.

That function is called by getItemByProduct function inside class Mage_Sales_Model_Quote, so if you don't want control over option level in items, you can try re-writing that method too. Product being added to the cart will be available as the $product object. This function is called from the function _addCatalogProduct in same class, which holds the qty as well. This is the qty which is requested to be added into the cart in current request cycle.

  • If I do Mage::log(count($this->getAllItems())) in function getItemByProduct , and when I add a product to quote then it shows 0 count in system.log but I think count should be 1 and when I add second product in quote then count shows 1 and shows 2 on adding third item. Why it is showing this behaviour.? Commented Mar 2, 2016 at 11:30
  • It's because that function is executed before actually adding product to cart. It controls whether the product being added to cart should be added as a new item or it should increase qty of a previous item.
    – Prateek
    Commented Mar 2, 2016 at 11:35
  • If I am unable to retrieve item then how can I edit that item info? Commented Mar 2, 2016 at 11:39
  • @shashank, I have updated the answer with more details.
    – Prateek
    Commented Mar 2, 2016 at 11:45
  • Ok Is there not any event which can fulfill my need.? Commented Mar 2, 2016 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.