Complete code with customer registered , genearate order programatically, along with sales quotes, sales items and sales address <?php //http://www.magentocommerce.com/boards/viewthread/28426/P45/ require_once '../app/Mage.php'; $app = Mage::app(); Mage::register('isSecureArea', true); //no output before here, will get a session header error $customer_id = 222; //222 rmuthe $shopping_cart = array(); /* $part = array( array("PartId" => '1', "Quantity" => '1'), array("PartId" => '2', "Quantity" => '1') ); */ $part = array("PartId" => '1', "Quantity" => '2'); $part1 = array("PartId" => '2', "Quantity" => '2'); //$part= array(); $shopping_cart[] = $part; //repeat as necessary $shopping_cart[] = $part1; //repeat as necessary $params = array("AccountNo" => $customer_id, "PartCart" => $shopping_cart); $quote_pk = PrepareOrder($params); $order_pk = ConfirmOrder($quote_pk); echo "<br />Quote Id Generated : " . $quote_pk; echo "<br />Order Id Generated : " . $order_pk; echo "<br />DONE"; function PrepareOrder($params) { foreach ($params as $k => $v) { $$k = $v; } $customerObj = Mage::getModel('customer/customer')->load($AccountNo); $storeId = $customerObj->getStoreId(); $quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj); //sets ship/bill address $storeObj = $quoteObj->getStore()->load($storeId); $quoteObj->setStore($storeObj); $productModel = Mage::getModel('catalog/product'); foreach ($PartCart as $part) { foreach ($part as $k => $v) { $$k = $v; } $productObj = $productModel->load($PartId); //Modified Here annet-pk //$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj); try { $quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj); } catch (Exception $e) { echo $e; } $quoteItem->setQuote($quoteObj); $quoteItem->setQty($Quantity); $quoteObj->addItem($quoteItem); } /* $quoteObj->collectTotals(); $quoteObj->save(); */ $shippingMethod = 'flatrate_flatrate'; $quoteObj->getShippingAddress()->setShippingMethod($shippingMethod); $quoteObj->getShippingAddress()->setCollectShippingRates(true); $quoteObj->getShippingAddress()->collectShippingRates(); $quoteObj->collectTotals(); // calls $address->collectTotals(); $quoteObj->save(); $quoteId = $quoteObj->getId(); return $quoteId; } function ConfirmOrder($quoteId) { $hpc_connector_orderid = '786-2222222-3333333'; $hpc_connector_sitename = 'ebay'; //ebay / amazon //methods: authorizenet, paypal_express, googlecheckout, purchaseorder $hpc_payment_method = 'checkmo'; //methods: flatrate_flatrate, freeshipping_freeshipping $hpc_shipping_method = 'flatrate_flatrate'; $hpc_shipping_method_description = 'eBay Order Fullfilment'; $quoteObj = Mage::getModel('sales/quote')->load($quoteId); $items = $quoteObj->getAllItems(); $quoteObj->collectTotals(); $quoteObj->reserveOrderId(); $quotePaymentObj = $quoteObj->getPayment(); //methods: authorizenet, paypal_express, googlecheckout, purchaseorder $quotePaymentObj->setMethod($hpc_payment_method); $quoteObj->setPayment($quotePaymentObj); $convertQuoteObj = Mage::getSingleton('sales/convert_quote'); $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress()); $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj); $orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress())); //annet -pk to set shipping method // $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress())); $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress())) ->setShipping_method($hpc_shipping_method) ->setShippingDescription($hpc_shipping_method_description); $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment())); $orderObj->setHpcOrderId($hpc_connector_orderid); $orderObj->setHpcOrderFrom($hpc_connector_sitename); foreach ($items as $item) { //@var $item Mage_Sales_Model_Quote_Item $orderItem = $convertQuoteObj->itemToOrderItem($item); if ($item->getParentItem()) { $orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId())); } $orderObj->addItem($orderItem); } $orderObj->setCanShipPartiallyItem(false); $totalDue = $orderObj->getTotalDue(); //$orderObj->sendNewOrderEmail(); $orderObj->place(); //calls _placePayment $orderObj->save(); // $orderId = $orderObj->getId(); // return $orderId; $orderObj->load(Mage::getSingleton('sales/order')->getLastOrderId()); $lastOrderId = $orderObj->getIncrementId(); return $lastOrderId; echo "Recent Order Id :".$lastOrderId; } ?>
You must log in to post a comment.
Pingback: Open Source Rocks - Programmatically create order in Magento
Hi,
May I know which version of magento is this for? Is this compatible with V1.5.1?
Hi Homan,
Hope you are doing well.
This code is working fine for 1.5.1.0.
It have been tested on 1.5.1.0. and after that i have post this code.
If any more query, don’t hesitate to reply.
Hope this will help you.
🙂
Hello can i know how i can find HpcOrderId and HpcOrderFrom
or what is these fields please
Hello wisam,
hope you are doing well.
HpcOrderId and Hpcorderfrom are extra column i have added in ‘sales_flat’ tables.
It is just to add some extra info added as per my requirement.
You can ignore that field if you don’t want.
Pingback: Magento for noobies, dicas de artigos | Bruno PorKaria #mobiletalk
The above code is helpful to create order in admin side but it not decrement in product quantity.suppose the order was successfully created with respective product (Product1)with quantity 2 .When the order successful its doesn’t decrease the quantity value in product page.
Please mentions what is the issue ..
Hi Vijay,
I think following links can help you.
http://pragneshkaria.com/2011/08/30/programatically-change-products-quantity-after-order-cancelled-magento/
http://pragneshkaria.com/2011/08/09/hack-for-mass-update-products-timestamp-changes-magento/
Thanks..Vijay..!!
🙂