How to create order programmatically in magento 2?

Magento 2 You can create order programmatically by simple coding.

You need to Create custom quote for order and based on that you can convert the quote to Order in Magento 2 by a simple code.

Create order information from the template file to helper file.

In above order info, We take customer basic details and Items info.
We have taken a simple and configurable product item. For configurable product, We need to pass super_attribute array value. For Test purpose, I have taken Magento Sample data configurable and simple product id.

For Configurable, 93 is Color attribute id where 52 is Gray option id.
142 is size attribute id where 167 is for XS size option id, you need to pass the dynamic value for a configurable product.

Create helper file,

Let’s say Data.php file location at, app/code/Rbj/Training/Helper/Data.php

You got a result as array for success order place,¬†Array ( [success] => ‘OrderId’ )

2 Replies to “How to create order programmatically in magento 2?”

  1. Hi Rakesh, Can you tell about this error. I followed the above code and having this error (N.B: I am using Magento 2.3.2):

    Fatal error: Uncaught TypeError: Argument 2 passed to MagentoEavModelAttributeDataText::validateLength() must be of the type string, null given, called in /var/www/html/vendor/magento/module-eav/Model/Attribute/Data/Text.php on line 80 and defined in /var/www/html/vendor/magento/module-eav/Model/Attribute/Data/Text.php:141 Stack trace: #0 /var/www/html/vendor/magento/module-eav/Model/Attribute/Data/Text.php(80): MagentoEavModelAttributeDataText->validateLength(Object(MagentoCustomerModelAttribute), NULL) #1 /var/www/html/vendor/magento/module-eav/Model/Validator/Attribute/Data.php(131): MagentoEavModelAttributeDataText->validateValue(NULL) #2 /var/www/html/vendor/magento/framework/Validator/Constraint.php(54): MagentoEavModelValidatorAttributeData->isValid(Object(MagentoCustomerModelCustomerInterceptor)) #3 /var/www/html/vendor/magento/framework/Validator.php(59): MagentoFrameworkValidatorConstraint->isValid(Object(MagentoCustomerModelCustomerInterceptor)) #4 /var/www/html/vendor/magento/ in /var/www/html/vendor/magento/module-eav/Model/Attribute/Data/Text.php on line 141

Leave a Reply