0

How to implement the custom functionality without extension i.e Buy X Get Y free. If customer buy the 2(X) quantity then the third (Y) will free and the third product will automatically add in the cart with the amount zero(Free product)

If the free extension are available for this( Buy X Get Y free) please share the link.

Am using magento 2.2.2

3
  • I am not sure what you are looking for exactly. There are some built in functionality under marketing->car price rules in action area. But if you are looking for something more advanced you can use Amasty module amasty.com/free-gift-for-magento-2.html
    – Korbin
    Commented Sep 7, 2018 at 13:10
  • suppose the offer is Buy 1 get 1 free. Commented Sep 8, 2018 at 3:35
  • suppose the offer is Buy 1 get 1 free. If the customer purchase the 1 product i.e (xyz) product name and then he/she will get the 1 free product (that free product will auto add the same product in the cart) like this. prnt.sc/kry1se Commented Sep 8, 2018 at 3:42

1 Answer 1

-1

If you Add Free Product in Cart When Add Perticular Product In cart. So You Can Use Event Observer For Add Free Product in cart.

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="add_free_product_to_cart" instance="Vendor\Checkout\Observer\AddFreeProductToCart" />
    </event>
</config>

Observer: AddFreeProductToCart.php

<?php
namespace Vendor\Checkout\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Session as CheckoutSession;

class AddFreeProductToCart implements ObserverInterface
{
    protected $productRepository;
    protected $checkoutSession;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        CheckoutSession $checkoutSession
    ) {
        $this->productRepository = $productRepository;
        $this->checkoutSession = $checkoutSession;
    }

    public function execute(Observer $observer)
    {
        $item = $observer->getEvent()->getData('quote_item');
        $product = $item->getProduct();
       
        if ($product->getSku() == '306081-0142') {

            // Add the desired product (SKU '620001-01') to the cart
            $productIdToAdd = '620001-01';

            $productToAdd = $this->productRepository->get($productIdToAdd, false, null, true);

            if ($productToAdd && $productToAdd->getId()) {

                $quote = $this->checkoutSession->getQuote();
                $productToAdd->setPrice(0);
                $quote->addProduct($productToAdd);
                $quote->collectTotals();
                $quote->save();
            }
        }
    }
}

When You Use This Event-Observer Then Solve Your Problem For Buy X Get Y Free Product..

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.