Thursday 21 May 2015

Programatically create order in Magebto ?

HI sometime we need to create a orders grammatically in magento and with the below code you can easily


Create a new file in magento root and add the below code in that
<?php
require_once('../app/Mage.php'); //Path to Magento
umask(0);
Mage::app();


$productids=array(1,2); // products you want to add in order
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$order=1;
 // Start New Sales Order Quote
 $quote = Mage::getModel('sales/quote')->setStoreId($store->getId());

 // Set Sales Order Quote Currency
 $quote->setCurrency($order->AdjustmentAmount->currencyID);
 $customer = Mage::getModel('customer/customer')
             ->setWebsiteId($websiteId)
             ->loadByEmail('demo@gmail.com');
 if($customer->getId()==""){
     $customer = Mage::getModel('customer/customer');
     $customer->setWebsiteId($websiteId)
             ->setStore($store)
             ->setFirstname('rohit')
             ->setLastname('goel')
             ->setEmail('any@gmail.com')
             ->setPassword("123456");
     $customer->save();
 }

 // Assign Customer To Sales Order Quote
 $quote->assignCustomer($customer);

     // Configure Notification
 $quote->setSendCconfirmation(1);
 foreach($productids as $id){
     $product=Mage::getModel('catalog/product')->load($id);
     $quote->addProduct($product,new Varien_Object(array('qty'   => 1)));
 }

 // Set Sales Order Billing Address
 $billingAddress = $quote->getBillingAddress()->addData(array(
     'customer_address_id' => '',
     'prefix' => '',
     'firstname' => 'john',
     'middlename' => '',
     'lastname' =>'Deo',
     'suffix' => '',
     'company' =>'',
     'street' => array(
             '0' => 'Patiala',
             '1' => 'Sector 64'
         ),
     'city' => 'Noida',
     'country_id' => 'IN',
     'region' => 'PB',
     'postcode' => '147001',
     'telephone' => '9464532670',
     'fax' => 'gghlhu',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));
 $shipprice=10;
 // Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
     'customer_address_id' => '',
     'prefix' => '',
     'firstname' => 'Rohit',
     'middlename' => '',
     'lastname' =>'Goel',
     'suffix' => '',
     'company' =>'',
     'street' => array(
             '0' => 'Patiala',
             '1' => 'Sector 64'
         ),
     'city' => 'Patiala',
     'country_id' => 'IN',
     'region' => 'PB',
     'postcode' => '147001',
     'telephone' => '9464532670',
     'fax' => 'gghlhu',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));
 if($shipprice==0){
     $shipmethod='freeshipping_freeshipping';
 }

 // Collect Rates and Set Shipping & Payment Method
 $shippingAddress->setCollectShippingRates(true)
                 ->collectShippingRates()
                 ->setShippingMethod('flatrate_flatrate')
                 ->setPaymentMethod('checkmo');

 // Set Sales Order Payment
 $quote->getPayment()->importData(array('method' => 'checkmo'));

 // Collect Totals & Save Quote
 $quote->collectTotals()->save();

 // Create Order From Quote
 $service = Mage::getModel('sales/service_quote', $quote);
 $service->submitAll();
 //$increment_id = $service->getOrder()->getRealOrderId();

 // Resource Clean-Up
 //$quote = $customer = $service = null;

 // Finished
 //return $increment_id;



?>

No comments:

Post a Comment