if 2 items in cart if customer login both item should need to add cart , Both quote (before login & after login) merge and create final quote which contain all item of customer quote

Refference links that i got in google



  • Please clarify question more. because magento 2.2 provide default functionality.
    – Yogesh
    Commented Nov 2, 2017 at 9:41
  • is it possible if customer login add cart items to customer cart, if possible please explain, Using REST API. Commented Nov 3, 2017 at 6:46
  • @Yogesh My products API returns products, if i try with customer i am getting error, my url : error : snag.gy/0jbhTr.jpg can u help me
    – zus
    Commented Oct 23, 2018 at 11:05

2 Answers 2


By default, Magento 2 in API side does not provide any API for Merge guest cart with customer card when the customer is login.

But you can replace the guest cart with customer cart.

API : (/V1/carts/:cartId) 
File : vendor/magento/module-quote/Model/QuoteManagement.php
Function : public function assignCustomer($cartId, $customerId, $storeId)

But if you want to develop functionality Merge cart Live Magento web side you need to create custom API.


You need to create "Around" Plugin in your custom extension.


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MageKnight_Quote">
            <module name="Magento_Quote"/>            



use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'MageKnight_Quote', __DIR__);


<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <type name="Magento\Quote\Api\CartManagementInterface">
        <plugin name="mergeGuestCart"



namespace MageKnight\Quote\Plugin\Model;

use Magento\Framework\Exception\StateException;

 * Class CartManagement
class CartManagement
     * @var \Magento\Quote\Api\CartRepositoryInterface
    protected $quoteRepository;

     * @var \Magento\Customer\Api\CustomerRepositoryInterface
    protected $customerRepository;

     * @var \Magento\Customer\Model\CustomerFactory
    protected $customerModelFactory;

     * @var \Magento\Quote\Model\QuoteIdMaskFactory
    private $quoteIdMaskFactory;

     * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
     * @param \Magento\Customer\Model\CustomerFactory $customerModelFactory
     * @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
    public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\CustomerFactory $customerModelFactory,
        \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->customerRepository = $customerRepository;
        $this->customerModelFactory = $customerModelFactory;
        $this->quoteIdMaskFactory = $quoteIdMaskFactory;

     * Around plugin to assign customer to guest cart
     * @param \Magento\Quote\Api\CartManagementInterface $subject
     * @param callable $proceed
     * @param int $cartId The cart ID.
     * @param int $customerId The customer ID.
     * @param int $storeId
     * @return boolean
    public function aroundAssignCustomer(
        \Magento\Quote\Api\CartManagementInterface $subject,
        callable $proceed,
    ) {
        $quote = $this->quoteRepository->getActive($cartId);
        $customer = $this->customerRepository->getById($customerId);
        $customerModel = $this->customerModelFactory->create();

        if (!in_array($storeId, $customerModel->load($customerId)->getSharedStoreIds())) {
            throw new StateException(
                __("The customer can't be assigned to the cart. The cart belongs to a different store.")
        if ($quote->getCustomerId()) {
            throw new StateException(
                __("The customer can't be assigned to the cart because the cart isn't anonymous.")
        try {
            $customerActiveQuote = $this->quoteRepository->getForCustomer($customerId);
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            $customerActiveQuote = false;
        if ($customerActiveQuote) {
            /** Merge carts */
        /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
        $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'quote_id');
        if ($quoteIdMask->getId()) {
        return true;

Your Answer

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

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