I'm brand new to Magento 2 programming, although I have a number of years of PHP behind me.
I need to store an id from an external system alongside an order that that system creates.
I've got the code that creates the quote and turns it into an order working, alongside my custom payment method, but I can't get it to save my extra ID.
I've used and altered code found elsewhere on this site to create the order:
public function doCreate($dataArray, $order=null) {
foreach($dataArray->Orders as $orderData) {
$orderData=$orderData->Order;
}
$store=$this->storeManager->getStore();
$websiteId = $this->storeManager->getStore()->getWebsiteId();
$customer=$this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($orderData->ShippingInfo->Email);// load customet by email address
$customerName=explode(' ',(string)$orderData->ShippingInfo->RecipientName);
$firstName=$customerName[0];
$lastName=$customerName[count($customerName)-1];
echo "First $firstName Last $lastName \n";
if(!$customer->getEntityId()){
//If not avilable then create this customer
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($firstName)
->setLastname($lastName)
->setEmail((string)$orderData->ShippingInfo->Email)
->setPassword($orderData->ShippingInfo->Email);
$customer->save();
}
$quote=$this->quote->create(); //Create object of quote
$quote->setStore($store); //set store for which you create quote
// if you have allready buyer id then you can load customer directly
$customer= $this->customerRepository->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($customer); //Assign quote to customer
//add items in quote
foreach($orderData->OrderItems as $item){
$item=$item->Item;
$product=$this->product->load($item->SKU);
$product->setPrice($item->Price);
$quote->addProduct(
$product,
intval($item->Quantity)
);
}
//Set Address to quote
$shippingInfo=$orderData->ShippingInfo;
$address=array( //address Details
'firstname' => (string)$firstName,
'lastname' => (string)$lastName,
'street' => (string)$shippingInfo->Address1,
'city' => (string)$shippingInfo->City,
'country_id' => (string)$shippingInfo->Country,
'region' => (string)$shippingInfo->State,
'postcode' => (string)$shippingInfo->PostalCode,
'telephone' => (string)$shippingInfo->PhoneNumber,
'save_in_address_book' => 0
);
$quote->getBillingAddress()->addData($address);
$quote->getShippingAddress()->addData($address);
// Collect Rates and Set Shipping & Payment Method
$shippingAddress=$quote->getShippingAddress()
->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('freeshipping_freeshipping'); //shipping method $shippingInfo->Service
$quote->setPaymentMethod('cpayment'); //payment method
$quote->setInventoryProcessed(false); //not effetc inventory
$quote->save(); //Now Save quote and your quote is ready
// Set Sales Order Payment
$quote->getPayment()->importData(['method' => 'cpayment']);
// Collect Totals & Save Quote
$quote->collectTotals()->save();
// Create Order From Quote
$order = $this->quoteManagement->submit($quote);
$order->setEmailSent(0);
$increment_id = $order->getRealOrderId();
if($order->getEntityId()){
$result['order_id']= $order->getRealOrderId();
$order->addData('external_id',$dataArray->ExtId);//THIS IS THE ID I NEED TO SAVE
$order->save();
}else{
$result=['error'=>1,'msg'=>'Your custom message'];
}
echo "Complete: Final result is\n";
print_r($result);
return $result;
}
If I add debugging near the end, eg echo $order->getData('external_id')
then it shows up, but it's not saved in the database.
I guess I must be missing some XML somewhere, but as I said I'm brand new to Magento and finding the lack of documentation frustrating (mostly seems to be 'copy and paste this and it will work'!)
Thanks in advance...
$order->setData()
.sales_order
has columnexternal_id
?