0

Everything works well, but it adds for all by default, but I need it added for a specific customer, regardless of whether it’s active at the moment or not, I have a customer ID, but how can i add product to his card ?

    ...
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Catalog\Model\ProductRepository $productRepository,
    \Magento\Quote\Model\QuoteRepository $quoteRepository,

    ...
    $this->_cart = $cart;
    $this->_productRepository = $productRepository;
    $this->_quoteRepository = $quoteRepository;

    ...

public function autoAddToCart(){

  ....

try {
    $data = [
        'qty' => $products_count,
        'product' => $products_id,
        'bundle_option' => $selectionProds
         ];
     $this->_cart->addProduct($_product,$data);
     $this->_cart->save();
     $cartId = $this->_cart->getQuote()->getId();
     $quote = $this->_quoteRepository->getActive($cartId);
     $this->_quoteRepository->save($quote);
     $quote->collectTotals();
      }
      catch(\Exception $e) {
           echo  $e->getMessage();
      }
0

you need to assign customer to quote

$quote->assignCustomer($customer);

more info you can found https://webkul.com/blog/create-quote-and-order-programmatically-in-magento2/

hope it will help you

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.