Skip to main content
events.php is wrong need to create evnets.xml file in etc folder
Source Link

Adnan Answer worked for me.

In event.phpxml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xs  d">
    <event name="controller_action_predispatch_checkout_cart_add">
        <observer name="Name-Your-Observer" instance="[Vendor]\[Module]\Observer\[YourClassName]" />
    </event>
</config>

After that in Observer Folder Create YourClassName.php

<?php
namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class [YourClassName] implements ObserverInterface    
{
    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    private $actionFlag;
    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;

    /**
     * Data constructor.
     *
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\UrlInterface   $url
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\UrlInterface $url
    ) {

        $this->actionFlag = $actionFlag;
        $this->url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Stop further processing if your condition is met
        $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        // then in last redirect
        $observer->getControllerAction()->getResponse()->setRedirect($this->url->getUrl("[route]/[controller]/[action]"));

        return $this;
    }
}

Let me know if this helps and need all of the files.

Adnan Answer worked for me.

In event.php

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xs  d">
    <event name="controller_action_predispatch_checkout_cart_add">
        <observer name="Name-Your-Observer" instance="[Vendor]\[Module]\Observer\[YourClassName]" />
    </event>
</config>

After that in Observer Folder Create YourClassName.php

<?php
namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class [YourClassName] implements ObserverInterface    
{
    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    private $actionFlag;
    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;

    /**
     * Data constructor.
     *
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\UrlInterface   $url
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\UrlInterface $url
    ) {

        $this->actionFlag = $actionFlag;
        $this->url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Stop further processing if your condition is met
        $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        // then in last redirect
        $observer->getControllerAction()->getResponse()->setRedirect($this->url->getUrl("[route]/[controller]/[action]"));

        return $this;
    }
}

Let me know if this helps and need all of the files.

Adnan Answer worked for me.

In event.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xs  d">
    <event name="controller_action_predispatch_checkout_cart_add">
        <observer name="Name-Your-Observer" instance="[Vendor]\[Module]\Observer\[YourClassName]" />
    </event>
</config>

After that in Observer Folder Create YourClassName.php

<?php
namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class [YourClassName] implements ObserverInterface    
{
    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    private $actionFlag;
    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;

    /**
     * Data constructor.
     *
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\UrlInterface   $url
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\UrlInterface $url
    ) {

        $this->actionFlag = $actionFlag;
        $this->url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Stop further processing if your condition is met
        $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        // then in last redirect
        $observer->getControllerAction()->getResponse()->setRedirect($this->url->getUrl("[route]/[controller]/[action]"));

        return $this;
    }
}

Let me know if this helps and need all of the files.

Source Link

Adnan Answer worked for me.

In event.php

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xs  d">
    <event name="controller_action_predispatch_checkout_cart_add">
        <observer name="Name-Your-Observer" instance="[Vendor]\[Module]\Observer\[YourClassName]" />
    </event>
</config>

After that in Observer Folder Create YourClassName.php

<?php
namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class [YourClassName] implements ObserverInterface    
{
    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    private $actionFlag;
    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;

    /**
     * Data constructor.
     *
     * @param \Magento\Framework\App\ActionFlag $actionFlag
     * @param \Magento\Framework\UrlInterface   $url
     */
    public function __construct(
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\UrlInterface $url
    ) {

        $this->actionFlag = $actionFlag;
        $this->url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Stop further processing if your condition is met
        $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        // then in last redirect
        $observer->getControllerAction()->getResponse()->setRedirect($this->url->getUrl("[route]/[controller]/[action]"));

        return $this;
    }
}

Let me know if this helps and need all of the files.