Cart addProduct combining different configurable variations to same row
When using the addProduct function of cart to add different configurable variations to the cart they all combine to the first one added.
Steps to reproduce
Attributes
Color (id:1)
Red (id:1)
Blue (id:2)
Products
T-shirt (id:1)
Red t-shirt (id:2)
Blue t-shirt (id:3)
$product = $this->product->load(1);
$this->cart->addProduct($product, array(
'qty' => 1,
'super_attribute' => array(
1 => 1
)
));
$this->cart->addProduct($product, array(
'qty' => 1,
'super_attribute' => array(
1 => 2
)
));
$this->cart->save();
Expected result 1 red and 1 blue t-shirt in cart
Actual result 2 red t-shirts in cart
If these two addProduct calls were made in 2 separate requests the cart would contain 1 red and 1 blue
@nobuttsalex Hi, thank you for reporting this issue.
We are going to look into the issue you have described, in the mean time, please note that it is recommended to use Magento's API's when it is possible, in your case \Magento\Quote\Api\CartItemRepositoryInterface::save seems to be the most suitable.
If it is possible please try it and let us know of the results.
@ishakhsuvarov I don't think it's possible to do what I want with the API, possibly a bug? I was writing the code using the API and got this far:
$quote = $this->cart->getQuote();
$cart_item = $this->_objectManager->get('Magento\Quote\Api\Data\CartItemInterface');
$cart_item->setQuoteId($quote['entity_id']);
$cart_item->setQty(1);
$cart_item->setSku("SKU1");
$product_option = $this->_objectManager->get('Magento\Quote\Api\Data\ProductOptionInterface');
$extension_attributes = $this->_objectManager->get('Magento\Quote\Api\Data\ProductOptionExtensionInterface');
//add logic for setting product options here
$product_option->setExtensionAttributes($extension_attributes);
$cart_item->setProductOption($product_option);
$this->quoteItemRepository->save($cart_item);
The code breaks at trying to create a ProductOptionExtensionInterface with the error
`PHP Fatal error: Uncaught Error: Cannot instantiate interface Magento\\Quote\\Api\\Data\\ProductOptionExtensionInterface in /var/www/magento2/lib/internal/Magento/Framework/ObjectManager/Factory/AbstractFactory.php:99`
Should I raise this as an issue on here?
@nobuttsalex
Hi, please try getting Magento\Quote\Api\Data\ProductOptionExtensionFactory instead of Magento\Quote\Api\Data\ProductOptionExtensionInterface.
Factory's create method will provide you with the valid extension attributes object.
@ishakhsuvarov Thanks, using the API I have managed to do it. The issue I have now is getting the quote id. This code works if an item has already been added to the cart
$quote = $this->cart->getQuote();
$cart_item = $this->_objectManager->get('Magento\Quote\Api\Data\CartItemInterface');
$cart_item->setQuoteId($quote['entity_id']);
but for a new session $quote['entity_id'] is empty
@nobuttsalex I am not in the context of your extension flow, but looks like Quote for the user it not created or saved yet. You may look into methods of Magento\Quote\Api\CartManagementInterface if you need to create one.
@ishakhsuvarov Thanks, you were right I had to create the quote and assign it to the cart when it didn't exist. Last issue I have come across is the minicart does not update when adding products to the cart using the API. If I go to the cart and then update the quantity the minicart does start to show the items.
After adding item with API:

After updating quantity on cart page:

Here you can also find a solution to your issue: http://magento.stackexchange.com/questions/110315/magento-2-adding-multiple-configurable-product-variations-in-cart/116407#116407
The link above seems to contain proper solution.