Oct 22, 2012

Magento: Add products to placed order programatically

Ever wondered how to attach products to order programatically? It may require if you want to surprise your customer by giving them some special items along with their ordered products. Magento doesn’t allow you to do this, you need to write it through calling observer for event sales_order_place_after.

Copy this in the observer file which observes order place after event.

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $skuToAdd); //your product SKU to add 
$qty = 1;
$rowTotal = $product->getPrice();
$orderItem = Mage::getModel('sales/order_item')

Please note that you may also need to add the entry in sales_flat_quote_item table to be 100% sure it’s going to work for reorder also. If you’re not worried about reordering, the above code is fine.


  • Hi, could you also show some code about how to add an entry in the sales_flat_quote_item table as well?

  • Well, i appreciate this post but the information about this is minimum.
    What is the file in which you enter this code, i mean i not expert in magento, and found sales_order_place_after in 3 pages:
    In sales/model/order appear Mage::dispatchEvent(sales_order_place_after), put code her for try and not fuction.
    You say put source in the observer, this is mage/sales/model/observer.php here the event for sales_order_place_after is addVatRequestParamsOrderComment (mage/sales/etc/config.xml)
    And not function.


    • HI Nana. You will put this code in your custom module’s observer file for event sales_order_place_after. Never ever edit in core files!

      • Thanks you for response.

  • Hi,

    how can i make pages where user can add there products to the site. please suggest me how to make this.

  • Hello Kalpesh,

    How can i add custom option with it?


  • Thanks a lot!

    These 2 Lines:
    were all I needed, and missing on every other side!!!

    So yeah, THANKS!

  • Its adding product to the order but not updating the price.

  • Hello Kalpesh,

    This is adding product in the order but does not updating the order price .


Leave a comment


Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors