2

I have below observer

namespace Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ProductObserver implements ObserverInterface {

    protected $_urlManager;
    protected $_checkoutSession;
    protected $_cart;
    protected $_messageManager;
    protected $_redirect;
    protected $_request;
    protected $_response;
    protected $_responseFactory;
    protected $_resultFactory;
    protected $_scopeConfig;
    protected $_product;

    public function __construct(\Magento\Framework\UrlInterface $urlManager, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\App\Response\RedirectInterface $redirect, \Magento\Checkout\Model\Cart $cart, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\App\RequestInterface $request, \Magento\Framework\App\ResponseInterface $response, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Catalog\Model\Product $product, \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\Controller\ResultFactory $resultFactory
    ) {
        $this->_urlManager = $urlManager;
        $this->_checkoutSession = $checkoutSession;
        $this->_redirect = $redirect;
        $this->_cart = $cart;
        $this->_messageManager = $messageManager;
        $this->_request = $request;
        $this->_response = $response;
        $this->_responseFactory = $responseFactory;
        $this->_resultFactory = $resultFactory;
        $this->_scopeConfig = $scopeConfig;
        $this->_product = $product;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {

        //$observer->getRequest()->setParam('product', false); // Not working

        $controller = $observer->getControllerAction();
        $postValues = $this->_request->getPostValue();
        $cartQuote = $this->_cart->getQuote()->getData();
        $cartItemsCount = $this->_cart->getQuote()->getItemsCount();
        $cartItemsAll = $this->_cart->getQuote()->getAllItems();

        $this->_request->setParam('product', false); // Will not add product to Cart
        $this->_response->setRedirect($this->_redirect->getRefererUrl());
        $this->_checkoutSession->setNoCartRedirect(false);
    }
}

It gives error on

Fatal error: Call to a member function setParam() on null in

If I'm using

$this->_request->setParam('product', false); // Will not add product to Cart

Then also it adds product to the cart. It should not product to the cart right?

As $observer->getRequest()->setParam('product', false); not working need to use above ones.

1

You need to Inject \Magento\Framework\App\Request\Http like:

<?php
namespace Namespace\Module\Something;
class ClassName 
{
    protected $request;
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        ....//rest of parameters here
    ) {
       $this->request = $request;
       ...//rest of constructor here
    }
    public function getPost()
    {
        return $this->request->getPost();
    }
}

then you can get the values. It should work for you

|improve this answer|||||
  • By doing $this->getPost()->setParam('product', false); still it add product to cart. It gives Fatal error: Call to undefined method Zend\Stdlib\Parameters::setParam() – Ankit Shah Mar 7 '17 at 8:27
  • Try $this->request->setParam('product', false); – Shubham Mathur Mar 7 '17 at 8:35
  • I already tried seems not working – Ankit Shah Mar 7 '17 at 8:38
0

Refer to http://alanstorm.com/magento_dispatch_standard_router/
After that You should know why setParam is not working like you want.

|improve this answer|||||

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.