cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2.4.2 error when create order pragmatically

Magento2.4.2 error when create order pragmatically

I followed this tutorial to create order https://webkul.com/blog/create-quote-and-order-programmatically-in-magento2/. This solution is working fine till Magento2.3.5. I'm facing following issue with Magento2.3.6 and 2.4 versions. 

I've tried

cache:flush

di:compile 

 

Type Error occurred when creating object: Magento\InventoryIndexer\Model\Queue\ReservationData, Argument 2 passed to Magento\InventoryIndexer\Model\Queue\ReservationData::__construct() 
must be of the type int, null given, called in /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 121
3 REPLIES 3

Re: Magento2.4.2 error when create order pragmatically

Try This:
rm -rf var/generation/*
bin/magento setup:di:compile
bin/magento cache:clean

Re: Magento2.4.2 error when create order pragmatically

 

I've tried a viral solution and followed this https://webkul.com/blog/create-quote-and-order-programmatically-in-magento2/

The quote is created successfully but error occurred when place order through cronjob only.

$orderId = $this->cartManagementInterface->placeOrder($quote->getId());

And the following error occurred

 Type Error occurred when creating object: Magento\InventoryIndexer\Model\Queue\ReservationData, Argument 2 passed to Magento\InventoryIndexer\Model\Queue\ReservationData::__construct() 
must be of the type int, null given, called in /var/www/html/magen

Re: Magento2.4.2 error when create order pragmatically

Fix: 1 ->
Generally, the reason for this error is when you are passing arguments from the calling method but not receive that arguments in the called function.

For Example, I’ve passed two parameters in calling function and receive only one argument in a called function that means one argument is missing. This results in the error  : Type Error Occurred When Creating Object in Magento 2.

Sometimes, The Object is Created,  but because of the compilation issue, the object is not created in the generator. Generally, Many programmers Solve this by deleting the whole generated folder to resolve this issue.

However, it’s risky to remove that folder every time which may lead to data loss. Therefore, The solution of this error instead of removing that folder, Is try to run these commands in the sequence mentioned :

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush


Fix: 2 ->
Try to replace this line :

$orderId = $this->cartManagementInterface->placeOrder($quote->getId());

with this :

$order = $this->cartManagementInterface->submit($quote);

$quote --> Quote Object
$order --> returns Order Object

For Getting Order Id you can use :

$order->getIncrementId();

 

If my answer is useful, please Accept as Solution & give Kudos