0

I want to create orders from admin on behalf of a customer by using REST API. I have created cart and added items by using the below code.

$userData = array("username" => "admin", "password" => "admin123");
$baseUrl = 'http://test.com/';

$ch = curl_init($baseUrl."rest/V1/integration/admin/token");


curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);


/* Create empty cart for the guest customer  */
$ch =  curl_init($baseUrl."rest/V1/guest-carts/"); 

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$cartId = json_decode($result);

How can I assign customer to this cart,assign address and place the order? Please help me

0

Use below code

 $guestCart    = $this->getGuestQuote($cartId);
 $customerCart = $this->getCustomerQuote($token->getCustomerId());
 $customerCart->merge($guestCart)->collectTotals()->save();

and below function

   private function getGuestQuote($cartId)
{
    try {
        $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
        if ($quoteIdMask->getQuoteId()) {
            return $this->quoteRepository->get($quoteIdMask->getQuoteId());
        }
        $guestQuote = $this->quoteRepository->get($cartId);
    } catch (NoSuchEntityException $exception) {
        $this->logger->info($exception->getMessage());
        $guestQuote = '';
    }

    return $guestQuote;
} 

 private function getCustomerQuote($customerId)
{
    try {
        $customerQuote = $this->quoteRepository->getForCustomer($customerId);
    } catch (\Throwable $exception) {
        $this->logger->info($exception->getMessage());
        $store         = $this->storeManager->getStore();
        $cartId        = $this->cartManagement->createEmptyCart();
        $customerQuote = $this->quoteRepository->get($cartId);
        $customerQuote->setStore($store);
        $customer = $this->customerRepository->getById($customerId);
        $customerQuote->setCurrency();
        $customerQuote->assignCustomer($customer);
    }

    return $customerQuote;
}//end getCustomerQuote()
  • I want to Create order using rest API – Jancy Abraham Oct 14 at 8:23
  • Yes but you have to create plugin for Magento\Integration\Model\CustomerTokenService to achieve this. – Hitesh Agrawal Oct 15 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.