2

I want to restrict some products from add to cart and redirect to a cms page on Add to Cart action. I have used 'controller_action_predispatch_checkout_cart_add' event. The code restrict the add to cart action,but the redirection is not working. Here is my code

namespace NamaSpace\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class RestrictAddToCart implements ObserverInterface
{
protected $_request;  

protected $_product;  

protected $_responseFactory;

protected $_url;

public function __construct(
    RequestInterface $request,
    \Magento\Catalog\Model\Product $product,
    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url
)
{
    $this->_request = $request;
    $this->_product = $product;
    $this->_responseFactory = $responseFactory;
    $this->_url = $url;
}

/**
 * add to cart event handler.
 *
 * @param \Magento\Framework\Event\Observer $observer
 *
 * @return $this
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{   
    $productId = $observer->getRequest()->getParam('product');
    $product = $this->_product->load($productId); 

    if ($product->getPrescProduct()) {

        $observer->getRequest()->setParam('product', false);
        $customRedirectionUrl = $this->_url->getUrl('prescription-page');
        $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse();     exit;      
        return $this;
     }


return $this;
    }
}
  • The redirection is not working – Jancy Abraham Sep 18 '17 at 6:33
5
protected $responseFactory;
protected $url;

public function __construct(
    ...
    \Magento\Framework\App\ResponseFactory $responseFactory,
    \Magento\Framework\UrlInterface $url
    ...
) {
    $this->responseFactory = $responseFactory;
    $this->redirect = $redirect;
    $this->url = $url;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    //... Your Code

    $customRedirectionUrl = $this->url->getUrl('my-cms-page'); //Get url of cms page
    $this->responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse(); //Redirect to cms page
    die(); //This will stop execution and redirect to specific page
}
  • No, it is not working. – Jancy Abraham Sep 18 '17 at 6:32
  • Try after remove var or flush cache – Prince Patel Sep 18 '17 at 7:14
  • @PrincePatel,is it possible to redirect to product page using product url($Product->getProductUrl();) from observer – Hitesh Balpande Feb 6 at 7:36
  • @hiteshbalpande Yes, you can redirect to any URL from the observer – Prince Patel Feb 6 at 7:39
0

You can redirect using controlleraction of observer class,

public function execute(\Magento\Framework\Event\Observer $observer)
{   
    $productId = $observer->getRequest()->getParam('product');
    $product = $this->_product->load($productId); 

    if ($product->getPrescProduct()) {

        $observer->getRequest()->setParam('product', false);
        $customRedirectionUrl = $this->_url->getUrl('prescription-page');
        $observer->getControllerAction()->getResponse()->setRedirect($customRedirectionUrl);   
        exit;
     }

    return $this;
}
  • 1
    NetworkError: 500 Internal Server Error – Jancy Abraham Sep 18 '17 at 6:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.