Thursday, August 4, 2011

Programmetically Creat Order in Magento

Hello All,

Currently I am working on create order using   API data in magento .
so I write some code for how to create  order programmatic in magento.

this code for simple product and working on product sku.

  $sku="BProduct1";
  $buyInfo = array('qty' => 1);
 // $product[price]=$orderitems->UnitPrice;add this line  if you want to create your order as per custom price
  $quote->addProduct($ product , new Varien_Object($buyInfo));
  $product= Mage::getModel('catalog/product');

 $addressData = array(
                            'firstname' => 'Naresh',
                            'lastname' => 'Tank',
                            'street' => 'Chandkheda',
                            'city' => 'New jersey',
                            'postcode' => '07030',
                            'telephone' => '9995456855',
                            'country_id' => 'US',
                             'region_id' => 'NJ'
                         
                    );
            $billingAddress = $quote->getBillingAddress()->addData($addressData);
           $shippingAddress = $quote->getShippingAddress()->addData($addressData);
         
           $shippingAddress->setCollectShippingRates(true)
                            ->collectShippingRates()
                       ->setShippingMethod('flatrate_flatrate')
                       ->setPaymentMethod('purchaseorder');

  $quote->getPayment()->importData(array('method' => 'purchaseorder',
                                                           'po_number' => 956648787));
       
            $quote->collectTotals()->save();
         
            Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");
            $service = Mage::getModel('sales/service_quote', $quote);
            $service->submitAll();
         
            $new_incr_id = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId(Mage::app()->getStore('default')->getId());                              
         
            $order = Mage::getModel("sales/order")->loadByIncrementId(($new_incr_id-1));
                 
            $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
           
            if (!$invoice->getTotalQty()) {
             
                Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
         
            }
           
            $invoice->setRequestedCaptureCase(
            Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE
            );
            $invoice->register();
            $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder());
           
            $transactionSave->save();
         
            $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();  

here i consider billing address same as shipping address  u can set different address by passing other array in addData($addressData) method. 
here I use  purchaseorder as payment method and flate rat as shipping methode.