I am creating Add to cart API and is working fine with logged-in customer but I have following query:

Query:- Suppose as a guest I have added product to cart and now I am login then how to convert guest quote to customer quote.


You need to create a new merge cart API for this.


<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/mergeGuestCart" method="POST">
    <service class="WebbyTroops\MergeGuestCart\Api\MergeGuestCartInterface" method="mergeCart"/>
        <resource ref="self" />
        <parameter name="customerId" force="true">%customer_id%</parameter>


namespace WebbyTroops\MergeGuestCart\Api;

 * Interface MergeGuestCartInterface
 * @package WebbyTroops\MergeGuestCart\Api
interface MergeGuestCartInterface

     * Merge guest cart to logged in customer cart
     * @param string $guestQuoteId
     * @param int $customerId
     * @return bool
    public function mergeCart($guestQuoteId, $customerId);


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="WebbyTroops\MergeGuestCart\Api\MergeGuestCartInterface" type="WebbyTroops\MergeGuestCart\Model\MergeGuestCart" />


namespace WebbyTroops\MergeGuestCart\Model;

use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteRepository\SaveHandler;

class MergeGuestCart implements \WebbyTroops\MergeGuestCart\Api\MergeGuestCartInterface

public function __construct(
    CartRepositoryInterface $quoteRepository,
    QuoteIdMaskFactory $quoteIdMaskFactory,
    QuoteFactory $quoteFactory,
    SaveHandler $saveHandler
) {
    $this->quoteRepository = $quoteRepository;
    $this->quoteIdMaskFactory = $quoteIdMaskFactory;
    $this->saveHandler = $saveHandler;

 * {@inheritdoc}
public function mergeCart($guestQuoteId, $customerId){
    $quoteIdMask = $this->quoteIdMaskFactory->create()->load($guestQuoteId, 'masked_id');
    $guestQuote= $this->quoteRepository->get($quoteIdMask->getQuoteId());
    $quote= $this->quoteFactory->create()->loadByCustomer($customerId);

            return true;
        }catch(\Exception $e){
            throw new CouldNotSaveException(__($e->getMessage()));
        return false;


Once you add product to guest cart you will get quote mask id otherwise you can get this from quote_id_mast. You can test this in any REST client like postman. Request payload will be like

  "guest_quote_id" : "z7sMMVNXpEFFOGXM9A6zt5RijsM39dKE"

with endpoint(POST) as


Make sure you are sending customer token in header. Let me know if need further help.

  • thanks for update, is there any default magenro 2 API to merge guest quote with customer quote ? – Niranjan Gondaliya Oct 30 '18 at 4:48
  • there is no default API for merge cart – Ramkishan Suthar Oct 30 '18 at 4:51
  • Did it work for you? if yes then please accept this answer. it'll help others – Ramkishan Suthar Oct 30 '18 at 8:57
  • I am implementing i will accept answer once it complete and working. – Niranjan Gondaliya Oct 30 '18 at 9:06
  • SaveHandler is not work with API, bcoz SaveHandler is using Session. – Niranjan Gondaliya Oct 30 '18 at 10:10

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.