1

I want to assign items added to 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 in which I want to transfer the cart item already has a quote created on quote table. so I thought changing the quote_id of that customer on quote_item table would do the trick.

Let me explain by example:

Let us assume we have two customer with quote id 1 and 2. Customer 1 added a product to the cart so on 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 than 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 programatically 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 update 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 is 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 clearify what could have possibly gone wrong?

Actually I searched the whole internet and also posted question on stackoverflow about the migration of customer cart from one customer to another but still I haven't got a perfect working solution for it.

If someone could help me on this, it would be greatly appreciated.

Thanks.

  • What I don't understand is why you want to add this item in one person's cart to another user. – camdixon Dec 11 '16 at 21:18
  • That's my requirement.actually one customer should be able to assign products to another customer cart. – aton1004 Dec 12 '16 at 9:02
0

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.

  • Yes that could be one of the solution. can you post some code snippet. Actually I have complex bundle products with many options, so I guess doing that would be quite difficult. Can you post some code to verify your logic. THanks – aton1004 Dec 12 '16 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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