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
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress
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