I want to assign items added to the cart by one customer to the other customer's cart.
For this, I see two tables responsible for holding that data. One is table named "quote" and other is "Quote_item".The customer to which I want to transfer the cart item already has a quote created on the quote table. so I thought changing the quote_id of that customer on the quote_item
table would do the trick.
Let me explain by example:
Let us assume we have two customers with quote id 1 and 2.
Customer 1 added a product to the cart so on the quote_item table the item will be saved with quote id 1. so if I could change the quote id of that item to 2 then that product will be assigned to customer 2.
I tried it by manually changing to the database and it worked. But When I tried to update that table programmatically than it couldn't update the row. I think there is some kind of dependencies that I couldn't figure out. So I hope if someone could help me figure it out.
Below is the updated code that I have written.
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quoteItemModel = $_objectManager->create('Magento\Quote\Model\Quote\Item');
//filter the product with same quote id
$productCollection = $_objectManager->create('Magento\Quote\Model\ResourceModel\Quote\Item\Collection')->addFieldToFilter('quote_id',$quoteId);
if($products = $productCollection->getData()){
foreach ($products as $product) {
$itemId = $product['item_id'];
$data = array();
//just putting a static value to check if the data gets updated
$data['quote_id']='2';
//updating the quote_item table
$quoteItemModel->load($itemId)->addData($data);
try {
$quoteItemModel->setId($itemId)->save();
echo "Data updated successfully.";
} catch (Exception $e){
echo $e->getMessage();
}
}
I know the code works perfectly with other custom tables. But in this case, it is not updating the data.
Its gives below error:
Fatal error: Call to a member function getStoreId() on null in
E:\xampp\htdocs…\vendor\magento\module-quote\Model\Quote\Item\AbstractItem.php
on line 144
When I checked that file. The function is:
public function getProduct()
{
$product = $this->_getData('product');
if ($product === null && $this->getProductId()) {
$product = clone $this->productRepository->getById(
$this->getProductId(),
false,
$this->getQuote()->getStoreId()
);
$this->setProduct($product);
}
I don't understand what it has to do with the update of table quote_item. I think there are some foreign key constraints or some dependency which is why it is not letting me update the quote_id of quote_item table.
Can some genius mind please help me out here or clarify what could have possibly gone wrong?
I searched the whole internet and also posted questions on StackOverflow about the migration of customer carts from one customer to another but still, I haven't got a perfect working solution for it.
If someone could help me with this, it would be greatly appreciated.
Thanks.
Best Answer
could you explain more your task ? the customer 1 has to keep his quote and items ? and the customer 2 just have the same items than the customer 1 that's it? In this case just use the
addProduct
method which take 2 parameters ($product, $qty
).Get the items from customer 1's quote. Loop on it, and add product to your customer 2's quote.