Categories: Magento

How to create an order programmatically in Magento 2

To create an order programmatically in Magento 2, just follow these steps:

Step 1: Create an Instance of the Order Object
In your custom module or script, start by creating an instance of the order object:

“`php
use Magento\Framework\App\Bootstrap;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Address;
use Magento\Sales\Model\Order\Item;

// Include the Magento bootstrap file
require __DIR__ . ‘/app/bootstrap.php’;

// Initialize the bootstrap
$bootstrap = Bootstrap::create(BP, $_SERVER);

// Get the object manager instance
$objectManager = $bootstrap->getObjectManager();

// Start the store emulation
$appState = $objectManager->get(‘\Magento\Framework\App\State’);
$appState->setAreaCode(‘frontend’);

// Create an instance of the order object
$order = $objectManager->create(Order::class);
“`

Step 2: Set Basic Order Information
Set the basic information for the order, such as the store ID, customer ID, and currency:

“`php
// Set the store ID
$storeId = 1;
$order->setStoreId($storeId);

// Set the customer ID
$customerId = 1;
$order->setCustomerId($customerId);

// Set the currency code
$currencyCode = ‘USD’;
$order->setBaseCurrencyCode($currencyCode);
$order->setOrderCurrencyCode($currencyCode);
“`

Step 3: Set Billing and Shipping Addresses
Create instances of the billing and shipping address objects, and set the required information:

“`php
$billingAddress = $objectManager->create(Address::class);
$billingAddress->setData([
‘firstname’ => ‘John’,
‘lastname’ => ‘Doe’,
‘street’ => ‘123 Main St’,
‘city’ => ‘New York’,
‘country_id’ => ‘US’,
‘postcode’ => ‘10001’,
‘telephone’ => ‘1234567890’,
’email’ => ‘john.doe@example.com’,
‘address_type’ => ‘billing’
]);
$order->setBillingAddress($billingAddress);

$shippingAddress = $objectManager->create(Address::class);
$shippingAddress->setData([
‘firstname’ => ‘John’,
‘lastname’ => ‘Doe’,
‘street’ => ‘456 Elm St’,
‘city’ => ‘New York’,
‘country_id’ => ‘US’,
‘postcode’ => ‘10001’,
‘telephone’ => ‘1234567890’,
’email’ => ‘john.doe@example.com’,
‘address_type’ => ‘shipping’
]);
$order->setShippingAddress($shippingAddress);
“`

Step 4: Set Payment Method and Shipping Method
Set the payment and shipping methods for the order:

“`php
$paymentMethod = ‘checkmo’; // Payment method code
$order->setPaymentMethod($paymentMethod);
$order->setShippingMethod(‘flatrate_flatrate’); // Shipping method code
“`

Step 5: Create Order Items
Create instances of the order item object and set the required information for each item:

“`php
$item = $objectManager->create(Item::class);
$item->setData([
‘product_id’ => 1,
‘qty_ordered’ => 1,
‘price’ => 100,
‘row_total’ => 100,
‘product_type’ => ‘simple’,
]);
$order->addItem($item);
“`

Step 6: Set Order Totals
Set the total amounts for the order:

“`php
$order->setSubtotal(100);
$order->setGrandTotal(100);
“`

Step 7: Save the Order
Save the order to persist it in the database:

“`php
$order->place();
$order->save();
“`

That’s it! You have successfully created an order programmatically in Magento 2. You can extend this code and add more complex logic as per your requirements. Remember to handle exceptions and error handling appropriately in your implementation.

Article by: Web Designers Near Me | Author: Joseph Cozens – Senior Website Designer

With over 25 years experience, Joseph started as the leased line manager for an ISP in the 1990’s and built websites for the support team internally, but was often asked by corporate business customers if we could provide a website for their company. So in 1998 with people looking for website designers near me, he started the web design company building websites part time, and in 1999 started building websites full time. In the early 2000’s we decided to call ourselves DesignsOnline.co.uk Joseph built one of the first online medical schools allowing doctors worldwide to enrol and pay for medical statistics training via the internet… Joseph used to build bespoke content management systems for companies who needed to regularly update their websites and for bespoke ecommerce websites which were hand coded at the time.

Today Joseph develops and builds top level professional websites without the needlessly high price tag.
You can contact Joseph directly via WhatsApp on 07944062954.

Web Designers Near Me

With over 25 years experience, Joseph started as the leased line manager for an ISP in the 1990's and built websites for the support team internally, but was often asked by corporate business customers if we could provide a website for their company. So in 1998 with people looking for website designers near me, he started the web design company building websites part time, and in 1999 started building websites full time. In the early 2000's Joseph built one of the first online medical schools allow doctors worldwide to enrol and pay for medical statistics training via the internet.