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.