1

For this, I use below event observer

    <?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="servicefee" instance="deca\Servicefee\Observer\AddFeeProduct" />
    </event>

</config>

And below is my observer code `

<?php
namespace deca\Servicefee\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use deca\Servicefee\Helper\Data as servicefeeHelper;

class AddFeeProduct implements ObserverInterface
{
    protected $_productRepository;
    protected $_cart;
    protected $formKey;
    /**
    * @var servicefeeHelper
    */
     protected $servicefeeHelper;

    public function __construct(
      \Magento\Catalog\Model\ProductRepository $productRepository, 
      \Magento\Checkout\Model\Cart $cart, 
      \Magento\Framework\Data\Form\FormKey $formKey,
      servicefeeHelper $servicefeeHelper
     )
   {
        $this->_productRepository = $productRepository;
        $this->_cart = $cart;
        $this->formKey = $formKey;
        $this->servicefeeHelper = $servicefeeHelper;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $serviceFeeSku = trim($this->servicefeeHelper->getServiceFeeUPC());
        $item = $observer->getEvent()->getData('quote_item');
        $product = $observer->getEvent()->getData('product');
        $item = ($item->getParentItem() ? $item->getParentItem() : $item);
        $_product = $this->_productRepository->get($serviceFeeSku);

        //  Check to see if item is already in the cart
        $arrProdIds = $this->_cart->getQuoteProductIds();
        $serviceFeeProdID = $_product->getEntityId();
        foreach ($arrProdIds as $key=>$value)
        {
            if ($key == $serviceFeeProdID)
            {
               return;
            }
        }

        // Enter the id of the prouduct which are required to be added to avoid recurrsion
        if($product->getId() != $serviceFeeProdID){
            $params = array(
                'product' => $serviceFeeProdID,
                'qty' => 1
            );

            $this->_cart->addProduct($_product,$params);
            $this->_cart->save();
        }

    }
}

Then I tried to add any product but whenver I click to addtocart button it redirects to the non-visible product URl with 404 error. Product also not added to the cart.Can anybody help me to understand my issue?

  • Most likely there occurs an error during addProductcall. One situation can be that your free product is not salable, but there are many other prossibilities. I would suggest to debug addProduct() in Quote model - that's where most of the logic is implemented. – HelgeB Mar 8 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.