Magento – Add multiple products to cart programatically – magento 2.1

cartmagento-2.1

I have created a custom module and add multiple products in cart on Ajax request. I follow a default Magento code for adding product to cart. But every time Magento throws below error.

Cart %1 does not contain item %2

This error is throws from

vendor/magento/module-quote/Model/Quote/Item/CartItemPersister.php

I have debugged Magento code and found that it creates new quote every time and it only occurs when the customer is not logged in.

When a customer is logged in its working fine.

Best Answer

I found this to be an issue with using: \Magento\Quote\Model\QuoteFactory and \Magento\Quote\Model\QuoteManagement whilst creating my order. I resolved this by using the CartRepositoryInterface and CartManagementInterface instead.

I encountered this problem attempting to programmatically create an order. You may be able to engineer a solution based on my code as I imagine at it's root, it's the same issue.

Blog post here: http://frankclark.xyz/magento2-1-programatically-create-order