1

I want to move all cart items of a current customer with another customer(move all cart items to another customer cart items).
In other words, I want to share One Customer Cart items with another customer. How this possible in Magento 2?

Here is my code but not working (just added one button and cart page and on click, I call below code)

<?php

namespace Vendor\MultiUserAccounts\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
use Magento\Quote\Model\QuoteFactory;
class Update extends \Magento\Framework\App\Action\Action {
    protected $cart;
    protected $checkoutSession;
    protected $cartHelper;
    protected $request;
    protected $helper;
    protected $quoteFactory;
    protected $productRepository;
    protected $messageManager;
    /**/
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Checkout\Helper\Cart $cartHelper,
        \Magento\Framework\App\Request\Http $request,
        \Vendor\MultiUserAccounts\Helper\Data $helper,
        \Magento\Quote\Api\CartRepositoryInterface  $quoteFactory,
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Framework\Message\ManagerInterface $messageManager
    ){
        $this->checkoutSession = $checkoutSession;
        $this->cart = $cart;
        $this->resultPageFactory = $resultPageFactory;
        $this->cartHelper = $cartHelper;
        $this->request = $request;
        $this->helper=$helper;
        $this->quoteFactory = $quoteFactory;
        $this->productRepository = $productRepository;
        $this->messageManager = $messageManager;
        parent::__construct($context);
    }
    public function execute() {
        try {
            $quote_id=$this->helper->getQuoteId();
            $customer_id=$this->helper->getSubCustomerId();
            if($quote_id!='' && $customer_id!=''){
              /*Code for assign quote to another customer*/

                $current_quote = $this->checkoutSession->getQuote();
                $current_quote = $this->quoteFactory->get($current_quote->getId());
                $current_quote->setCustomerEmail('[email protected]');
                $current_quote->setCustomerFirstname('test');
                $current_quote->setCustomerLastname('test');
                $current_quote->setCustomerId($customer_id);

                //$current_quote->save();
                $this->quoteFactory->save($current_quote);
                $this->checkoutSession->clearQuote()->clearStorage();

            }
        } catch (\Exception $e) {
            $this->messageManager->addError(__("Error while update quote item ! :".$e->getMessage()));
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setPath('checkout/cart/index');
            return $resultRedirect;
        }
    }
    /**/
}

1 Answer 1

0

I think you've set required properties on the $current_quote quote object but not saved it, which is causing problem.

Add $current_quote->save(); after $current_quote->setCustomerId($customer_id); it should resolve the issue.

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.