Skip to content

Instantly share code, notes, and snippets.

@brankoajzele
Created May 24, 2012 12:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save brankoajzele/2781404 to your computer and use it in GitHub Desktop.
Save brankoajzele/2781404 to your computer and use it in GitHub Desktop.
Programatically create Magento order with tablerate shipping
<?php
require_once 'app/Mage.php';
Mage::app();
$quote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore('default')->getId());
$product = Mage::getModel('catalog/product')->load(6); /* 6 => Some product ID */
$buyInfo = array('qty' => 1);
$quote->addProduct($product, new Varien_Object($buyInfo));
$billingAddress = array(
'firstname' => 'Branko',
'lastname' => 'Ajzele',
'company' => 'Inchoo',
'email' => 'branko@inchoo.net',
'street' => array(
'Sample Street Line_1',
'Sample Street Line_2'
),
'city' => 'City',
'region_id' => '',
'region' => 'State/Province',
'postcode' => '12345',
'country_id' => 'NL',
'telephone' => '1234567890',
'fax' => '123456987',
'customer_password' => '',
'confirm_password' => '',
'save_in_address_book' => '0',
'use_for_shipping' => '1',
);
$quote->getBillingAddress()
->addData($billingAddress);
$quote->getShippingAddress()
->addData($billingAddress)
->setShippingMethod('tablerate_bestway')
->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();
//$order = $service->getOrder();
@priyambadamishra
Copy link

Hi,
Not working for me. Facing error {"status":false,"data":"Please specify a shipping method.","msg":"Request unsuccessfull"} for table rate shipping method.

setWebsiteId(Mage::app()->getStore()->getWebsiteId())->load($customerId); // echo '
'; print_r($customer->getPrimaryBillingAddress()->getData());
			// die;
			$customerQuote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId())->loadByCustomer($customerId);
					 // Set Sales Order Billing Address
 $billstreet = $customer->getPrimaryBillingAddress()->getStreet();
 $billingAddress = $customerQuote->getBillingAddress()->addData(array(
     'customer_address_id' => $customer->getPrimaryBillingAddress()->getId(),
     'prefix' => '',
     'firstname' => $customer->getPrimaryBillingAddress()->getFirstname(),
     'middlename' => '',
     'lastname' =>$customer->getPrimaryBillingAddress()->getLastname(),
     'suffix' => '',
     'company' =>'', 
     'street' => array(
             '0' => $billstreet[0],
             '1' => ''
         ),
     'city' => $customer->getPrimaryBillingAddress()->getCity(),
     'country_id' => 'IN',
     'region' => $customer->getPrimaryBillingAddress()->getRegion(),
     'postcode' => $customer->getPrimaryBillingAddress()->getPostcode(),
     'telephone' => $customer->getPrimaryBillingAddress()->getTelephone(),
     'fax' => '',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));
 
 // Set Sales Order Shipping Address
 $Shippstreet = $customer->getPrimaryShippingAddress()->getStreet();
$shippingAddress = $customerQuote->getShippingAddress()->addData(array(
     'customer_address_id' => $customer->getPrimaryShippingAddress()->getId(),
     'prefix' => '',
     'firstname' => $customer->getPrimaryShippingAddress()->getFirstname(),
     'middlename' => '',
     'lastname' =>$customer->getPrimaryShippingAddress()->getLastname(),
     'suffix' => '',
     'company' =>'', 
     'street' => array(
             '0' => $Shippstreet[0],
             '1' => ''
         ),
     'city' => $customer->getPrimaryShippingAddress()->getCity(),
     'country_id' => 'IN',
     'region' => $customer->getPrimaryShippingAddress()->getRegion(),
     'postcode' => $customer->getPrimaryShippingAddress()->getPostcode(),
     'telephone' => $customer->getPrimaryShippingAddress()->getTelephone(),
     'fax' => '',
     'vat_id' => '',
     'save_in_address_book' => 1
 ));



     //$shipmethod='freeshipping_freeshipping';

     $shipmethod= "tablerate_bestway";

			 	$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
					->setShippingMethod($shipmethod)
					->setPaymentMethod($paymethod);
				$customerQuote->getPayment()->importData(array('method' => $paymethod));
				$customerQuote->collectTotals()->save();
				$customerQuote->setReservedOrderId(NULL);
				$service = Mage::getModel('sales/service_quote', $customerQuote);
				$service->submitAll();
				$order = $service->getOrder();
                $iddd = $order->getIncrementId();
                $order->save();
				$customerQuote->setIsActive(0)->save();


				$Data = array('order_id' => $order->getIncrementId(),'subtotal'=> $order->getSubtotal(),'grandtotal' => $order->getGrandTotal(), 'itmecount'=> count($order->getData()) ,'paymentmethod'=> $paymethod);
				echo json_encode(array("status" => true,  "data" => $Data, "msg"=> "Request successfull"));
				
				 if($paymethod =='cashondelivery'){
					 $order->sendNewOrderEmail();
				}
				
		} catch (Exception $e) {

				echo json_encode(array("status" => false,  "data" => $e->getMessage(), "msg"=> "Request unsuccessfull"));
		
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment