Magento – programatically creating order

apicartmagento-1

I am programatically creating order for regsitered user but I keep on getting error. How can I fix it? Below is my code:

Error:

Fatal error: Call to a member function getTypeInstance() on a
non-object in
D:\wamp\www\mournewcs3\app\code\core\Mage\Sales\Model\Quote.php on
line 968

$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
if (1) {
    // for customer orders:
    $customer = Mage::getModel('customer/customer')
        ->setWebsiteId(1)
        ->loadByEmail('gaurav.mishra@abc.com');
    $quote->assignCustomer($customer);
}

$product = Mage::getModel('catalog/product')->load(363);
$buyInfo = array(
    'qty' => 2,
);
$quote->addProduct($product, new Varien_Object($buyInfo));
$quote->addProduct($product2, new Varien_Object($buyInfo));
$addressData = array(
    'firstname' => 'Test',
    'lastname' => 'Test',
    'street' => 'Sampl',
    'city' => 'fff',
    'postcode' => '123456',
    'telephone' => '123456',
    'country_id' => 'IN',
);
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
    ->setShippingMethod('flatrate_flatrate')
    ->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->collectTotals()->save();
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();
printf("Created order %s\n", $order->getIncrementId());

Best Answer

    $productids=array(1,2,4,3);
 $websiteId = Mage::app()->getWebsite()->getId();
 $store = Mage::app()->getStore();
 // 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($email);
 if($customer->getId()==""){
     $customer = Mage::getModel('customer/customer');
     $customer->setWebsiteId($websiteId)
             ->setStore($store)
             ->setFirstname('Jhon')
             ->setLastname('Deo')
             ->setEmail($email)
             ->setPassword("password");
     $customer->save();
 }

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

     // Configure Notification
 $quote->setSendCconfirmation(1);
 foreach($productsids 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' => 'Noida',
             '1' => 'Sector 64'
         ),
     'city' => 'Noida',
     'country_id' => 'IN',
     'region' => 'UP',
     'postcode' => '201301',
     'telephone' => '78676789',
     'fax' => 'gghlhu',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));

 // Set Sales Order Shipping Address
$shippingAddress = $quote->getShippingAddress()->addData(array(
     'customer_address_id' => '',
     'prefix' => '',
     'firstname' => 'john',
     'middlename' => '',
     'lastname' =>'Deo',
     'suffix' => '',
     'company' =>'', 
     'street' => array(
             '0' => 'Noida',
             '1' => 'Sector 64'
         ),
     'city' => 'Noida',
     'country_id' => 'IN',
     'region' => 'UP',
     'postcode' => '201301',
     'telephone' => '78676789',
     '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;
Related Topic