Magento – Magento 2.1.8 Api merge the guest items to customer quote

checkoutcustomermagento2postmanquote

I would like to implement am Magento 2 REST api i.e

if the customer login then we have to merge the guest items to
customer quote.

As per the requirement i found one file in the respected path i.e

/vendor/magento/module-checkout/Model/Session.php

public function loadCustomerQuote()
    {
        if (!$this->_customerSession->getCustomerId()) {
            return $this;
        }

        $this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]);

        try {
            $customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            $customerQuote = $this->quoteFactory->create();
        }
        $customerQuote->setStoreId($this->_storeManager->getStore()->getId());

        if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
            if ($this->getQuoteId()) {
                $this->quoteRepository->save(
                    $customerQuote->merge($this->getQuote())->collectTotals()
                );
            }

            $this->setQuoteId($customerQuote->getId());

            if ($this->_quote) {
                $this->quoteRepository->delete($this->_quote);
            }
            $this->_quote = $customerQuote;
        } else {
            $this->getQuote()->getBillingAddress();
            $this->getQuote()->getShippingAddress();
            $this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())
                ->setTotalsCollectedFlag(false)
                ->collectTotals();
            $this->quoteRepository->save($this->getQuote());
        }
        return $this;
    }

how do we implement this functionality in magento 2 REST api.

1) Is it possible to implement?

2) if it is possible, how to check with the postman?

3) is it possible to consider the session concept in REST apis?

Please suggest me and help me.

Thanks

Best Answer

Magento 2 REST can not distinguish between authenticated and anonymous customers. Web services ACL is applied for admin users only. All customers are anonymous for REST services.

You can create such REST service if you want but you need to authenticate customers by yourself (using \Magento\Customer\Model\Session, for example). Of course, REST requests should contain PHPSESSID cookie for appropriate session in HTTP headers.

Related Topic