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;
}
}
/**/
}