0

I want to configurable product add to cart programmatically in magento 2 to customer cart

2 Answers 2

0

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.

1
  • I think he needs to add it programmatically ? Commented Feb 13, 2018 at 11:46
0

You can use following approach for the solution :

First we need to load a customer for ex :

$customer->loadByEmail($orderData['email']);

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

2
  • thanks for answer but i want to add product to customer cart Commented Feb 13, 2018 at 11:37
  • Please check my update answer for a better idea on how to do it. Commented Feb 13, 2018 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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