This is how I solved my above problem. I hope this will help others too.
Below is the code of my controller file.
I think the code is self explanatory also please refer to the comments added on the code. Hope this will provide a walkaround.
<?php
namespace Custom\Subscription\Controller\Index;
class Assign extends \Magento\Framework\App\Action\Action {
protected $resultPageFactory;
protected $quote;
protected $messageManager;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Checkout\Model\Cart $quote,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
){
$this->resultPageFactory = $resultPageFactory;
$this->getQuote = $quote;
$this->customerFactory = $customerFactory;
$this->messageManager = $messageManager;
$this->customerRepository = $customerRepository;
parent::__construct($context);
}
public function execute(){
//die('I am working on this.');
$customerSession = $this->_objectManager->get('Magento\Customer\Model\Session');
if(!$customerSession->isLoggedIn()) {
$this->_redirect('customer/account/');
}
$data = $this->getRequest()->getParams('id');
$customerId = $data['id'];
$quotes = $this->getQuote->getQuote();
$quoteId = $quotes->getEntityId();
// var_dump($quote->getQuoteId());die();
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
try{
//try direct sql
//take quote id of customer to assign here
//get the quote id from quote table. there may be multiple quote of customer so take the last one
$collecion = $_objectManager->create('Magento\Quote\Model\ResourceModel\Quote\Collection')->addFieldToFilter('customer_id',$customerId);
$lastitem = $collecion->getLastItem();
//check if current customer has product in his cart.
$itemCollection = $_objectManager->create('Magento\Quote\Model\ResourceModel\Quote\Item\Collection')->addFieldToFilter('quote_id',$quoteId);
if(!count($itemCollection->getData())){
$this->messageManager->addError( __('There are no products on the cart.Please add products to cart before assigning to customer.') );
$this->_redirect('subscription/index/');
}else{
//this will return the last Quote id of that customer
$customerquoteId = $lastitem->getEntityId();
$resource = $_objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('quote_item'); //gives table name with prefix
//Update Data into table
$sql = "Update " . $tableName . " Set quote_id = ".$customerquoteId." where quote_id =".$quoteId ;
//var_dump($sql);die();
$connection->query($sql);
$this->messageManager->addSuccess( __('You have successfully assigned the product.') );
//logout the coach
// $customerSession->logout();
$this->_redirect('subscription/index/');
}
//ends here
}catch (\Exception $e) {
$message = $this->messageManager->addError(
__($e->getMessage())
);
$this->_redirect('subscription/index/');
}
}
}
There are few tables that should concern you: quote, quote_item. Please go through the table to understand how magento saves the item in cart.
I am using the direct SQL queries because magento don't let me update quote_item through model. Not sure if it the best way of achieving it.
Hope this will help!!