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.
@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.
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 This is my first Magento programming and it's hard for me to deal with.
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?
@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.