Trouble creating a quote/order through custom code (programmatically create order without use API)

Today’s post is most important. When you create a custom order programmatically , then this post is for you. Magento is really beautiful if you use it otherwise it is horrible 🙂 .I always love to hack magento. So lets start. Bellow this code is only for loged In user.

This example for only “direct post type payment method”, don’t try any payment method that’s redirect to payment site.

This example cover only simple and downloadable product

// get logged in cusomer id
$customerAccountNo = Mage::getModel('customer/session')->getCustomer()->getId();
// load customer object
$customerObj = Mage::getModel('customer/customer')->load($customerAccountNo);
// assign this customer to quote object, before any type of magento order, first create quote.
$quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
$quoteObj = $quoteObj->setStoreId(Mage::app()->getStore()->getId());

// product id
$productId = 167;
$productModel = Mage::getModel('catalog/product');
$productObj = $productModel->load($productId);

// for simple product
if ($productObj->getTypeId() == 'simple') {
    $quoteObj->addProduct($productObj , 1);
    // for downloadable product
} else if ($productObj->getTypeId() == 'downloadable') {
    $params = array();
    $links = Mage::getModel('downloadable/product_type')->getLinks( $productObj );
    $linkId = 0;
    foreach ($links as $link) {
        $linkId = $link->getId();
    $params['product'] = $productId;
    $params['qty'] = $qty;
    $params['links'] = array($linkId);
    $request = new Varien_Object();
    $quoteObj->addProduct($productObj , $request);
// sample billing address
$billingAddress = array
    'email' => NULL,
    'firstname' => "FirstName",
    'lastname' => "LastName",
    'telephone' => "102920",
    'street' => "517 4th Avenue",
    'country_id' => "US",
    'city' => 'San Diego',
    'postcode' => "90001",
    'region_id' => "12",
    'region' => 'California',
    'company' => "Company",
    'fax' => "123445",
    'customer_address_id' => NULL,

$quoteBillingAddress = Mage::getModel('sales/quote_address');

//if product is not virtual
if (!$quoteObj->getIsVirtual()) {
    $shippingAddress = $billingAddress;
    $quoteShippingAddress = Mage::getModel('sales/quote_address');
    // fixed shipping method


$transaction = Mage::getModel('core/resource_transaction');
if ($quoteObj->getCustomerId()) {

$ccInfo = array();
// assign payment method
$payment_method = 'authorizenet';
if ($payment_method == "authorizenet") {
    $quotePaymentObj = $quoteObj->getPayment();
    $cc_number = '4111111111111111';
} else {
    $quotePaymentObj = $quoteObj->getPayment();

$convertQuoteObj = Mage::getSingleton('sales/convert_quote');
if ($quoteObj->getIsVirtual()) {
    $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getBillingAddress());
} else {
    $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());

$orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);

if (!$quoteObj->getIsVirtual()) {

// set payment options
if (count($ccInfo) > 0) {
    $cc_number = '4111111111111111';
} else {


foreach ($items as $item) {
//@var $item Mage_Sales_Model_Quote_Item
    $orderItem = $convertQuoteObj->itemToOrderItem($item);
    if ($item->getParentItem()) {


$totalDue = $orderObj->getTotalDue();

$transaction->addCommitCallback(array($orderObj, 'place'));
$transaction->addCommitCallback(array($orderObj, 'save'));

try {
} catch (Exception $e){
    Mage::throwException('Order Cancelled Bad Response from Credit Authorization.');


Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$orderObj, 'quote'=>$quoteObj));


9 thoughts on “Trouble creating a quote/order through custom code (programmatically create order without use API)

  1. Thank you for this example, I thought it was impossible to find something working concerning create / convert quotes / orders…

  2. Hey it works for me like charm,,,but how to do the same with profile id because the i don’t have card details ,i have only profileid,by using that i am able access the last four numbers of the card…..
    Please suggest me on this….looking forward to reply.

    1. Hi,

      This post is only cover credit card process. In your case you need to modified payment method for placing order through profile id. There are many extension in market. So pick them one and modify as your requirement.


      1. Thanks for response,
        As you suggested some extensions are there for web application.
        But i am trying to do this for mobile applications.
        Can you please suggest me how to overcome this.

  3. Thank you!

    Is it possible to create without payment method because I dont use payment I am using this for product, which has price 0$

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.