Hi,
I recently migrated to Magento 2.3 from Netsuite. Now I want to sync orders from Netsuite to Magento using the API. I have gone through the documentation and found that I need to follow few steps to create an order for a customer. But following those steps we will not be able to sync orders for the registered customers on Magento as we will not have their passwords to get the token of the customer and create an order.
Is there any alternate approach to sync the orders from third party application to Magento for existing customers?
Thanks.
Hello @vamshikris6b3e ,
For that, you must debug code from admin to create the customer process. Because, when a user creates the customer from the admin system will not ask for a password. so I suggest to go through that.
And as per my debug in admin Magento uses Magento\Customer\Api\CustomerRepositoryInterface to save customer object without a password.
Here is the few code snap from vendor/magento/module-customer/Controller/Adminhtml/Index/Save.php
Method name : Magento\Customer\Controller\Adminhtml\Index\Save::execute()
Screenshot : https://prnt.sc/s4si5q
Code Snippet
$customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray( $customer, $customerData, \Magento\Customer\Api\Data\CustomerInterface::class ); $this->_customerRepository->save($customer);
$this->_customerRepository : this variable is an object of Magento\Customer\Api\CustomerRepositoryInterface
Problem Solved: Click on Kudos and Accept as Solution.
Thank you
Hiren Patel
Hi @Hiren_K_Patel ,
Thanks for the response, but my problem is users are already registered from the website registration process, there we have the password given by users in the registration process. Now I need to sync the Order for these users who are registered in the portal using the API.
Thanks.
Hello @vamshikris6b3e ,
If you are looking for an order create from API then the following code will help you.
<?phpnamespace YourNameSpace\ModuleName\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper{ /** * @param Magento\Framework\App\Helper\Context $context * @param Magento\Store\Model\StoreManagerInterface $storeManager * @param Magento\Catalog\Model\Product $product * @param Magento\Framework\Data\Form\FormKey $formKey $formkey, * @param Magento\Quote\Model\Quote $quote, * @param Magento\Customer\Model\CustomerFactory $customerFactory, * @param Magento\Sales\Model\Service\OrderService $orderService, */ public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Product $product, \Magento\Framework\Data\Form\FormKey $formkey, \Magento\Quote\Model\QuoteFactory $quote, \Magento\Quote\Model\QuoteManagement $quoteManagement, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Sales\Model\Service\OrderService $orderService ) { $this->_storeManager = $storeManager; $this->_product = $product; $this->_formkey = $formkey; $this->quote = $quote; $this->quoteManagement = $quoteManagement; $this->customerFactory = $customerFactory; $this->customerRepository = $customerRepository; $this->orderService = $orderService; parent::__construct($context); } /** * Create Order On Your Store * * @param array $orderData * @return array * */ public function createMageOrder($orderData) { $store=$this->_storeManager->getStore(); $websiteId = $this->_storeManager->getStore()->getWebsiteId(); $customer=$this->customerFactory->create(); $customer->setWebsiteId($websiteId); $customer->loadByEmail($orderData['email']);// load customet by email address if(!$customer->getEntityId()){ //If not avilable then create this customer $customer->setWebsiteId($websiteId) ->setStore($store) ->setFirstname($orderData['shipping_address']['firstname']) ->setLastname($orderData['shipping_address']['lastname']) ->setEmail($orderData['email']) ->setPassword($orderData['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['items'] as $item){ $product=$this->_product->load($item['product_id']); $product->setPrice($item['price']); $quote->addProduct( $product, intval($item['qty']) ); } //Set Address to quote $quote->getBillingAddress()->addData($orderData['shipping_address']); $quote->getShippingAddress()->addData($orderData['shipping_address']); // Collect Rates and Set Shipping & Payment Method $shippingAddress=$quote->getShippingAddress(); $shippingAddress->setCollectShippingRates(true) ->collectShippingRates() ->setShippingMethod('freeshipping_freeshipping'); //shipping method $quote->setPaymentMethod('checkmo'); //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' => 'checkmo']); // 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(); }else{ $result=['error'=>1,'msg'=>'Your custom message']; } return $result; } }
Example parameters:
$tempOrder=[ 'currency_id' => 'USD', 'email' => 'test@webkul.com', //buyer email id 'shipping_address' =>[ 'firstname' => 'jhon', //address Details 'lastname' => 'Deo', 'street' => 'xxxxx', 'city' => 'xxxxx', 'country_id' => 'IN', 'region' => 'xxx', 'postcode' => '43244', 'telephone' => '52332', 'fax' => '32423', 'save_in_address_book' => 1 ], 'items'=> [ //array of product which order you want to create ['product_id'=>'1','qty'=>1], ['product_id'=>'2','qty'=>2] ] ];
Problem Solved: Click on Kudos and Accept as Solution
Thank you
Hiren Patel
Hi @Hiren_K_Patel ,
This helper is create a new password for the customer while creating a quote. This will be a issue to update the existing customer password. Please correct me if missed any.
Thanks.
Hi @vamshikris6b3e,
You can migrate your orders from NetSuite to Magento by using the CSV file. As I know, NetSuite allows you to export your data to CSV files. After that, you can import those files to Magento, please remember to check the compatibility between two platforms.
During the import process, it could be possible to sync your orders with registered customers through customer emails. However, this process is quite complicated and requires technical skills.
If you need further assistance, you can send me a sample of your CSV file and I will see what i can do. Hope this can help you!