Pages

Monday, 23 June 2014

Programmatically create an order in Magento

Here I will explain how to create an order programmatically in magento.

As it turns out, there are two “types” of order creation process. One is called One Page Checkout or as I like to call it “frontend order creation” and other one is “admin order creation”. Frontend order creation relies heavily on AJAX-ed approach for almost anything, from getting shipping calculations from shipping gateways to handling credit card processing from payment gateways. On one hand it’s a simple, yet extremely complex process to trace and try to simulate into straight forward order creation by your custom code. Little less complex is the admin order creation, or at least it looks like less complex.

Basically if you as an admin try to create an order from Magento, you have these certain steps where you first choose the customer for which you wish to create order, then you choose store, then you choose product, shipping method, payment method, etc. Each of this steps actually manipulates current session values as we are talking about AJAX-ed behavior. So basically Magento models internally use session reading for setting the necessary values in place of having the direct methods by which you yourself can set those values like customer id, store id, etc.

With enough time on your side, strong will and determination one can monitor, analyze and trace the process of such order creation in order to try to execute it with it’s own custom code.
Below you will find an example of code that does exactly that, it programmatically creates order in Magento.



        $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
        $product = Mage::getModel('catalog/product')->load(1); /* 6 => Some product ID */
        $buyInfo = array('qty' => 1);
        $quote->addProduct($product, new Varien_Object($buyInfo));
        $billingAddress = array(
            'firstname' => 'Manoj',
            'lastname' => 'Kumar',
            'company' => 'Test',
            'email' =>  'mail@gmail.com',
            'street' => array(
                'Sample Street Line_1',
                'Sample Street Line_2'
            ),
            'city' => 'City',
            'region_id' => '',
            'region' => 'State',
            'postcode' => 'postcode',
            'country_id' => 'CountryID',
            'telephone' =>  '06063065831',
            'fax' => '123456987',
            'customer_password' => 'password',
            'confirm_password' =>  'password',
            'save_in_address_book' => '0',
            'use_for_shipping' => '1',
        );
        $quote->getBillingAddress()
            ->addData($billingAddress);
        $quote->getShippingAddress()
            ->addData($billingAddress)
            ->setShippingMethod('flatrate_flatrate')
            ->setPaymentMethod('checkmo')
            ->setCollectShippingRates(true)
            ->collectTotals();
        $quote->setCheckoutMethod('guest')
            ->setCustomerId(null)
            ->setCustomerEmail($quote->getBillingAddress()->getEmail())
            ->setCustomerIsGuest(true)
            ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
        $quote->getPayment()->importData( array('method' => 'checkmo'));
        $quote->save();
        $service = Mage::getModel('sales/service_quote', $quote);
        $service->submitAll();
        $this->_redirect('test/index/index');
   

When Execute above code order will be placed successfully on magento.

Programmatically create an order in Magento

Please study the above code well. Code covers only one “combination”, adding simple product with one custom option to cart, using “Check/Money” payment method and flat rate shipping.

There are far more complex combinations you might need. Hope this code serves you as a good starting point.

Please leave any comments or questions.

ThankQ.

 

No comments:

Post a Comment