<?php
class MageCheck_Tutorial_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this-> createOrder();
}
public function createOrder()
{
// ...
}
}
/**
* @var \Magento\Quote\Model\Quote $quote
*/
protected $_quote;
/**
* @var \Magento\Sales\Model\Order $order
*/
protected $_order;
/**
* @var object \Magento\Catalog\Model\Product $product
*/
protected $_product;
/**
* @var \Magento\Store\Model\StoreManagerInterface $storeManager
*/
protected $_customer;
public function __construct(
\Magento\Quote\Model\Quote $quote,
\Magento\Sales\Model\Order $order,
\Magento\Catalog\Model\Product $product,
\Magento\Customer\Model\Customer $customer
){
$this->_quote = $quote;
$this->_order = $order;
$this->_product = $product;
$this->_customer = $customer;
public function execute()
{
$this->createOrder();
}
public function createOrder()
{
// ...
}
public function createOrder()
{
$productids = array(1, 2, 4, 3);
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$quote = Mage::getModel('sales/quote')->setStoreId($store->getId());
$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('Mage')
->setLastname('Check')
->setEmail($email)
->setPassword('password');
$customer->save();
}
$quote->assignCustomer($customer);
$quote->setSendCconfirmation(1);
foreach ($productids as $id) {
$product = Mage::getModel('catalog/product')->load($id);
$quote->addProduct($product, new Varien_Object(array('qty' => 1)));
}
$billingAddress = $quote->getBillingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => 'mage',
'middlename' => '',
'lastname' => 'Check',
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'New York',
'1' => 'Sector 64'
),
'city' => 'New York',
'country_id' => 'US',
'region' => 'NC',
'postcode' => '201301',
'telephone' => '78676789',
'fax' => 'gghlhu',
'vat_id' => '',
'save_in_address_book' => 1
));
$shippingAddress = $quote->getShippingAddress()->addData(array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => 'mage',
'middlename' => '',
'lastname' => 'Check',
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'New York',
'1' => 'Sector 02'
),
'city' => 'New York',
'country_id' => 'US',
'region' => 'NC',
'postcode' => '201301',
'telephone' => '1234567890',
'fax' => '1234567890',
'vat_id' => '',
'save_in_address_book' => 1
));
if ($shipprice == 0) {
$shipmethod = 'freeshipping_freeshipping';
}
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate')
->setPaymentMethod('checkmo');
$quote->getPayment()->importData(array('method' => 'checkmo'));
$quote->collectTotals()->save();
try {
$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$increment_id = $service->getOrder()->getRealOrderId();
} catch (Exception $ex) {
echo $ex->getMessage();
} catch (Mage_Core_Exception $e) {
echo $e->getMessage();
}
$quote = $customer = $service = null;
return $increment_id;
}
public function createOrder()
{
$order = [
'currency_id' => 'USD',
'email' => 'example@magecheck.com',
'shipping_address' => [
'firstname' => 'Mage',
'lastname' => 'Check',
'street' => '1st Street',
'city' => 'New York',
'country_id' => 'US',
'region' => 'NC',
'postcode' => '85001',
'telephone' => '123456789',
'fax' => '123456789',
'save_in_address_book' => 1
],
'items' => [
['product_id' => '1', 'qty' => 1],
['product_id' => '2', 'qty' => 2],
['product_id' => '3', 'qty' => 1],
['product_id' => '4', 'qty' => 3],
['product_id' => '5', 'qty' => 2],
['product_id' => '6', 'qty' => 1]
],
];
$customer = $this->_customer;
$quote = $this->_quote;
$customerRepository = $this->_customerRepository;
$product = $this->_product;
$store = '1';
$customer->setWebsiteId(1);
if (!$customer->getEntityId()) {
$customer->setCustomerId($customer->getId())
->setFirstname($order['shipping_address']['firstname'])
->setLastname($order['shipping_address']['lastname'])
->setEmail($order['email'])
->setPassword($order['email']);
$customer->save();
}
$quote->setStoreId($store);
$customer = $this->_customerRepository;
foreach ($order['items'] as $item)
{
$product = $this->_product->load($item['product_id']);
$product->setPrice('20');
$quote->addProduct($product, intval($item['qty']));
}
$quote->getBillingAddress()->addData($order['shipping_address']);
$quote->getShippingAddress()->addData($order['shipping_address']);
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping');
$quote->setPaymentMethod('checkmo');
$quote->setInventoryProcessed(false);
$quote->save();
$quote->getPayment()->importData(['method' => 'checkmo']);
$quote->collectTotals();
$quote->save();
$orderdata = $this->_quote;
$orderdata->save();
$orderdata->setEmailSent(0);
if ($orderdata->getEntityId()) {
$result['order_id'] = $orderdata->getRealOrderId();
} else {
$result = ['error' => 1, 'msg' => 'Your custom message'];
}
return $result;
}
This is the sample way to Programmatically create Orders in Magento!
Have you decided on an online Business Plan website?