I want to configurable product add to cart programmatically in magento 2 to customer cart
Find the customer in the backend, click on edit. You should see a tab on the left called shopping cart. You would expect that it should work from there. But it doesn't. Or at least I didn't find a way to make it work.
But here is how I did. Click on the 'Create order' from the customer edit screen. Select a store view to associate the order with. Then add to the order the products you want to put in the cart. After adding them, you will see for each line a column named Action. Select from the dropdown for each product "Move to shopping cart". Then hit "Update items and qty's". That's it.
You can use following approach for the solution :
First we need to load a customer for ex :
then we need to create an empty cart and quote as follows :
$cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart $quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote $quote->setStore($store); $customer= $this->customerRepository->getById($customer->getEntityId()); $quote->setCurrency(); $quote->assignCustomer($customer);
after above you need to add products to the quote
$options = array( 90 => 53, 143 => 170 ); $params = array( 'product' => $productId, 'super_attribute' => $options, 'qty' => $qty ); $_product = $this->productRepository->getById($productId); $quote->addProduct($_product,$params); $quote->save();
Where productRepository is an instance of \Magento\Catalog\Api\ProductRepositoryInterface