I want to create an order programmatically using Cron jobs, the codes run perfectly but when creating the order i got 'Error: 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 121string'
<?php
namespace Tech\System\Cron;
use Magento\Framework\HTTP\Client\Curl; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\StoreManagerInterface; use Magento\Quote\Api\CartManagementInterface; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Quote\Model\QuoteFactory; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Customer\Api\AddressRepositoryInterface; use Magento\Customer\Model\CustomerFactory; use Magento\Store\Model\App\Emulation; use Magento\InventoryApi\Api\StockRepositoryInterface; use Magento\Quote\Api\CartRepositoryInterface;
class OrderIntegration {
/** * @var \Magento\Framework\HTTP\Client\Curl */ protected $curlClient;
/**
* @var string
*/
protected $urlPrefix = 'https://';
/**
* @var string
*/
protected $apiUrl = 'myApi Url';
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
protected $quoteManagement;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
protected $quoteFactory;
protected $productRepository;
/**
* @var AddressRepositoryInterface
*/
protected $addressRepository;
protected $customerFactory;
protected $stockRepository;
protected $cartRepository;
/**
* @param StoreManagerInterface $storeManager
* @param Curl $curl
* @param CartManagementInterface $cartManagement
* @param QuoteFactory $quoteFactory
* @param AddressRepositoryInterface $addressRepository
* @param CustomerRepositoryInterface $customerRepository
* @param ProductRepositoryInterface $productRepository
* @param CustomerFactory $customerFactory
* @param StockRepository $stockRepository
*/
public function __construct( StoreManagerInterface $storeManager, CartManagementInterface $cartManagement, CustomerRepositoryInterface $customerRepository, QuoteFactory $quoteFactory, ProductRepositoryInterface $productRepository, AddressRepositoryInterface $addressRepository, Curl $curl, CustomerFactory $customerFactory, CartRepositoryInterface $cartRepository, StockRepositoryInterface $stockRepository ) { $this->storeManager = $storeManager; $this->quoteManagement = $cartManagement; $this->customerRepository = $customerRepository; $this->quoteFactory = $quoteFactory; $this->productRepository = $productRepository; $this->addressRepository = $addressRepository; $this->curlClient = $curl; $this->customerFactory = $customerFactory; $this->cartRepository = $cartRepository; $this->stockRepository = $stockRepository; }
public function getApiUrl()
{
return $this->urlPrefix . $this->apiUrl;
}
public function execute() {
$apiUrl = $this->getApiUrl();
$this->getCurlClient()->addHeader("Content-Type", "application/json");
//$this->getCurlClient()->setOption(CURLOPT_SSL_VERIFYHOST,false);
//$this->getCurlClient()->setOption(CURLOPT_SSL_VERIFYPEER,false);
$this->getCurlClient()->get($apiUrl);
$response = json_decode($this->getCurlClient()->getBody());
//$ola = json_encode($response);
//$ten = json_decode($ola);
$texts = $response[1];
//var_dump($texts);
//die();
foreach($texts as $text) {
if(empty($text->items)) {
continue;
}
//var_dump($text->items[0]->productsku);
//die();
// Data Information
$orderData =[
'email' => $text->member->email, //customer email id
'currency_id' => 'USD',
'address' =>[
'firstname' => $text->billingAddress->firstname,
'lastname' => $text->billingAddress->lastname,
'prefix' => '',
'suffix' => '',
'street' => $text->billingAddress->address,
'city' => $text->billingAddress->city,
'country_id' => 'US',
'region' => 'california',
'region_id' => '12', // State region id
'postcode' => '12345',
'telephone' => '',
'fax' => '',
'save_in_address_book' => 1
],
'items'=>
[
//simple product
[
'product_sku' => $text->items[0]->productsku,
'qty' => $text->items[0]->quantity
],
]
];
$store = $this->storeManager->getStore();
$storeId = $store->getStoreId();
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer = $customer->loadByEmail($orderData['email']);
if(!$customer->getId()){
//if customer is not exist then skip that customer
continue;
}
$quote = $this->quoteFactory->create();
$quote->setStore($store);
$customer= $this->customerRepository->getById($customer->getId());
$quote->setCurrency();
$quote->assignCustomer($customer); // Quote assign to customer
try {
$product = $this->productRepository->get($text->items[0]->productsku);
var_dump("Product Created with SKU" ."" . $product->getId() . "". "Successfully");
$quote->addProduct($product,intval($text->items[0]->quantity));
} catch (NoSuchEntityException $e) {
var_dump("No such product");
continue;
}
//Ending of Foreach loop for getting product
//Starting of Billing and Shipping
$billingAddress = null;
$shippingAddress = null;
try {
$billingAddress = $this->addressRepository->getById($customer->getDefaultBilling());
$shippingAddress = $this->addressRepository->getById($customer->getDefaultShipping());
var_dump("customer Found");
}
catch (NoSuchEntityException $e) {Var_dump("Customer Not Found");
continue;
}
if ($billingAddress) {
$quote->getBillingAddress()->importCustomerAddressData($billingAddress);
var_dump("it is a cool game");
}
if ($shippingAddress) {
$quote->getShippingAddress()->importCustomerAddressData($shippingAddress);
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('flatrate_flatrate');
var_dump("cool");
}
//set shipping method
$quote->setPaymentMethod('checkmo'); //set payment method
$quote->setInventoryProcessed(false);
$quote->save(); //quote save
$quote->getPayment()->importData(['method' => 'checkmo']);
// Collect Quote Total and Save
if($quote->collectTotals()->save()) {
var_dump("Are you joking?");
}
// Create Order From Quote Object
try {
$order = $this->quoteManagement->submit($quote);
var_dump("Is that real");
} catch (\Exception $e) {
// Log or handle the exception
echo "Error: " . $e->getMessage();
}
//order id for placed order
$orderId = $order->getIncrementId();
if($orderId){
$result['success']= $orderId;
var_dump("Successfully Created Cheers");
}else{
$result=['error'=>true,'msg'=>'something went wrong when Order placed'];
}
return $result;
//Ending of Billing and Shipping
}
}
/**
* @return Curl
*/
public function getCurlClient()
{
return $this->curlClient;
}
}