Programmatically create order in Magento 3 comments


Finally we have reached Some more closer to Order Import Programatically.

To Run this code You have to create one file in Magento Root Directory and after that Run it on your browser.

<?php

require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$id=3; // get Customer Id
$customer = Mage::getModel('customer/customer')->load($id);

$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);

$order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setStoreId($storeId)
->setQuoteId(0)
->setGlobal_currency_code('INR')
->setBase_currency_code('INR')
->setStore_currency_code('INR')
->setOrder_currency_code('INR');
//Set your store currency USD or any other

// set Customer data
$order->setCustomer_email($customer->getEmail())
->setCustomerFirstname($customer->getFirstname())
->setCustomerLastname($customer->getLastname())
->setCustomerGroupId($customer->getGroupId())
->setCustomer_is_guest(0)
->setCustomer($customer);

// set Billing Address
$billing = $customer->getDefaultBillingAddress();
$billingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setCustomerId($customer->getId())
->setCustomerAddressId($customer->getDefaultBilling())
->setCustomer_address_id($billing->getEntityId())
->setPrefix($billing->getPrefix())
->setFirstname($billing->getFirstname())
->setMiddlename($billing->getMiddlename())
->setLastname($billing->getLastname())
->setSuffix($billing->getSuffix())
->setCompany($billing->getCompany())
->setStreet($billing->getStreet())
->setCity($billing->getCity())
->setCountry_id($billing->getCountryId())
->setRegion($billing->getRegion())
->setRegion_id($billing->getRegionId())
->setPostcode($billing->getPostcode())
->setTelephone($billing->getTelephone())
->setFax($billing->getFax());
$order->setBillingAddress($billingAddress);

$shipping = $customer->getDefaultShippingAddress();
$shippingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setCustomerId($customer->getId())
->setCustomerAddressId($customer->getDefaultShipping())
->setCustomer_address_id($shipping->getEntityId())
->setPrefix($shipping->getPrefix())
->setFirstname($shipping->getFirstname())
->setMiddlename($shipping->getMiddlename())
->setLastname($shipping->getLastname())
->setSuffix($shipping->getSuffix())
->setCompany($shipping->getCompany())
->setStreet($shipping->getStreet())
->setCity($shipping->getCity())
->setCountry_id($shipping->getCountryId())
->setRegion($shipping->getRegion())
->setRegion_id($shipping->getRegionId())
->setPostcode($shipping->getPostcode())
->setTelephone($shipping->getTelephone())
->setFax($shipping->getFax());

$order->setShippingAddress($shippingAddress)
->setShipping_method('flatrate_flatrate');
/*->setShippingDescription($this->getCarrierName('flatrate'));*/
/*some error i am getting here need to solve further*/

//you can set your payment method name here as per your need
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod('purchaseorder')
->setPo_number(' – ');
$order->setPayment($orderPayment);

// let say, we have 2 products
//check that your products exists
//need to add code for configurable products if any
$subTotal = 0;
$products = array(
    '1' => array(
    'qty' => 2
    ),
    '2' => array(
    'qty' => 1
    )
);

foreach ($products as $productId=>$product) {
$_product = Mage::getModel('catalog/product')->load($productId);
$rowTotal = $_product->getPrice() * $product['qty'];
$orderItem = Mage::getModel('sales/order_item')
->setStoreId($storeId)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setProductId($productId)
->setProductType($_product->getTypeId())
->setQtyBackordered(NULL)
->setTotalQtyOrdered($product['rqty'])
->setQtyOrdered($product['qty'])
->setName($_product->getName())
->setSku($_product->getSku())
->setPrice($_product->getPrice())
->setBasePrice($_product->getPrice())
->setOriginalPrice($_product->getPrice())
->setRowTotal($rowTotal)
->setBaseRowTotal($rowTotal);

$subTotal += $rowTotal;
$order->addItem($orderItem);
}

$order->setSubtotal($subTotal)
->setBaseSubtotal($subTotal)
->setGrandTotal($subTotal)
->setBaseGrandTotal($subTotal);

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

Still there are limitation in this code.
LIMITATION

SOLVED:

http://pragneshkaria.com/2011/08/11/genearate-order-programatically-magento-along-with-sales-quotes-sales-items-and-sales-address/

 

Still need to solve further.

 

 

 1>Check if Customer Email-Id present or not. 2>If present then assign $id=3; // get Customer Id as done in code Else Add new Customer along with his Data(first name,last name , password, email id , Shipping and billing address) also check customer wants to Register or want to checkout as a Guest 3>Set code accordingly if Multi currency or Multi Store / Catalog present. 4>Set and Check Product Added in order are present or not other wise set error log. 

If any one use this code and get more closer solution you can free to share here.

Thanks to : http://blog.magentoconnect.us/magento-create-order-programmatically/

Now we are trying to solve all above limitation with following link
http://nightlyworker.posterous.com/programatically-create-customer-and-order-in

Report 2011.07.19 Tuesday (In time 2.00PM)

http://inchoo.net/ecommerce/magento/magentos-onepage-checkout-in-a-nutshell/

Create Order By SOAP
and then delete customer
http://www.magentocommerce.com/boards/viewthread/28426/P45/

--------------------------------------------------------
Create customer
http://www.magentocommerce.com/boards/viewthread/28426/P30/
------------------------------------------------------
API PHP
http://www.magentocommerce.com/boards/viewthread/28426/P60/

------------------------------------------------------
$order->setCreatedAt('2005-01-01 23:00:00');

$date = new Zend_Date($unixTimestamp);
$order->setCreatedAt($data->toString('Y-M-d H:m:s'));
http://www.magentocommerce.com/boards/viewthread/28426/P75/
-------------------------------------------------------
 current order import script
 http://www.magentocommerce.com/boards/viewthread/28426/P75/
 -------------------------------------------------------

 GUEST CHECKOUT
 http://www.magentocommerce.com/boards/viewthread/28426/P75/

 -----------------------------------------------------------
 importorders.php /guestOrders.php / MarkyCreateOrder.php
 http://www.magentocommerce.com/boards/viewthread/28426/P90/
 -------------------------------------------------------

 order page 9
 http://www.magentocommerce.com/boards/viewthread/28426/P120/

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools


3 thoughts on “Programmatically create order in Magento