Oct 22, 2012
kalpesh

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')
        ->setStoreId($order->getStore()->getStoreId())
        ->setQuoteItemId(NULL)
        ->setQuoteParentItemId(NULL)
        ->setProductId($product->getId())
        ->setProductType($product->getTypeId())
        ->setQtyBackordered(NULL)
        ->setTotalQtyOrdered($qty)
        ->setQtyOrdered($qty)
        ->setName($product->getName())
        ->setSku($product->getSku())
        ->setPrice($product->getPrice())
        ->setBasePrice($product->getPrice())
        ->setOriginalPrice($product->getPrice())
        ->setRowTotal($rowTotal)
        ->setBaseRowTotal($rowTotal)
        ->setOrder($order);
$orderItem->save();

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.

9 Comments

  • 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.

    Thanks.

    • 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

  • Thanks a lot!

    These 2 Lines:
    ->setOrder($order);
    $orderItem->save();
    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 .

    Thanks

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!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions