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

4
  • I hope this answer is help for you. magento.stackexchange.com/a/136160/70565 Commented Oct 14, 2019 at 5:38
  • @SweetyMasmiya How can I assign customer to this cart? Commented Oct 14, 2019 at 5:50
  • Do you have customer already ? Commented Oct 14, 2019 at 8:11
  • @HiteshAgrawal Yes, I have customerId Commented Oct 14, 2019 at 8:22

1 Answer 1

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()
2
  • I want to Create order using rest API Commented Oct 14, 2019 at 8:23
  • Yes but you have to create plugin for Magento\Integration\Model\CustomerTokenService to achieve this. Commented Oct 15, 2019 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.