cancel
Showing results for 
Search instead for 
Did you mean: 

Adding an additional product to the cart

Adding an additional product to the cart

Hello I am looking for hints on the web, but what I find does not work. I need to add an additional Virtual Product with Individual Options which it identifies under the SKU. He programmed the checkout_cart_product_add_afte event and I have an Observer file. However, I can not cope with adding this product after clicking add to cart. Help me please because this is my first Magento 2 programming and I can't handle it.

Thank you very much.

4 REPLIES 4

Re: Adding an additional product to the cart


@myEHtrip wrote:

Hello I am looking for hints on the web, but what I find does not work. I need to add an additional Virtual Product with Individual Options which it identifies under the SKU. He programmed the checkout_cart_product_add_afte event and I have an Observer file. However, I can not cope with adding this product after clicking add to cart. Help me please because this is my first Magento 2 programming and I can't handle it.

Thank you very much.


By automatically adding products to your customer’s cart, you remove friction from the purchasing process. Customers will be able to select and purchase products from your WooCommerce store in just a few clicks.

 

Re: Adding an additional product to the cart

I don't understand what this has to do with adding to cart in Magento 2? I have to add an additional product to the cart after clicking add to cart. I did an event for this purpose:

<event name = "checkout_cart_product_add_after">

I have code patches in the observer's file, but it didn't work.

class AddCartObserver implements ObserverInterface
{
    protected $_productRepository;
    protected $_cart;

    public function __construct(\Magento\Catalog\Model\ProductRepository $productRepository, \Magento\Checkout\Model\Cart $cart){
        $this->_productRepository = $productRepository;
        $this->_cart = $cart;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $item=$observer->getEvent()->getData('quote_item');
        $product=$observer->getEvent()->getData('product');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $product->getQty();

        $params = array(
            'product' => 1,
            'qty' => 1
        );

        $_product = $this->_productRepository->getById(1);
        $this->_cart->addProduct($_product,$params);
        $this->_cart->save();

    }
}

I have been looking for a long time on the internet how to do it and I have already entered a different code but none of it works Smiley Sad This is my first Magento programming and it's hard for me to deal with.

Re: Adding an additional product to the cart

I don't know if I did it right, but when I changed from checkout_cart_product_add_after to checkout_cart_add_product_complete, this code works and adds to the cart. Unfortunately, it does not update the number of products in the basket (icon next to the basket) and does not update the price in the entire basket. What can I do?

Re: Adding an additional product to the cart


@przemyslaw5e24 wrote:

Hello I am looking for hints on the web, but what I find does not work. I need to add an additional Virtual Product with Individual Options which it identifies under the SKU. He programmed the checkout_cart_product_add_afte event and I have an Observer file. However, I can not cope with adding this product after clicking add to cart. Help me please because this is my first Magento 2 programming and I can't handle it.

Thank you very much WestPenn Power.


it does not update the number of products in the basket and does not update the price in the entire basket.