0

I made modules that work fine but unable to redirect to another url when event fired

what code does when adding to cart is pressed I want to redirect to other URL say google.com

my module works as a log file is generated.

I also get error when i inspected console:

Fatal error: Call to undefined method Devshree\Affliliate\Observer\GetQuote::_redirect() in /var/www/html/magento/app/code/Devshree/Affliliate/Observer/GetQuote.php on line 15

below is my code :

Devshree\Affliliate\etc\frontend\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_save_before">
        <observer name="devshree_affliliate_event_obs" instance="Devshree\Affliliate\Observer\GetQuote" />
    </event>
</config>

Devshree\Affliliate\Observer\GetQuote.php

namespace Devshree\Affliliate\Observer;

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

    class GetQuote implements ObserverInterface {
        public function execute(Observer $observer) {
            $myfile = fopen("/var/www/html/magento/var/logdebug.log", "a+") or die("Unable to open file!");
            $abc = "heello";
            fwrite($myfile, print_r($abc,true));
            fclose($myfile);
            return $this->_redirect('*/auth/login');
        }
    }
5
  • I believe the redirect method is inherited when you're in a controller class, not an observer. You'd need to access the response object then call redirect on that
    – scrowler
    Commented Apr 17, 2016 at 18:06
  • Perhaps a plugin might be more suitable than an observer here?
    – scrowler
    Commented Apr 17, 2016 at 18:06
  • @RobbieAverill can you please edit my code to tell what should i do , i made it modules do you have some plugin so i can check
    – mcoder
    Commented Apr 17, 2016 at 19:15
  • Google it, try something then ask here if you can't get it to work. I have a question on here about observers vs plugins, look it up. They're similar only a plugin would let you hook into any public function not just Magento events
    – scrowler
    Commented Apr 17, 2016 at 19:21
  • ok can you will that way i tried using observer is worng can it be done via observer in someway
    – mcoder
    Commented Apr 17, 2016 at 19:37

1 Answer 1

3

You can redirect to external URL from observer like below :

namespace Devshree\Affliliate\Observer;

use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Response\RedirectInterface;

class GetQuote implements ObserverInterface {

    protected $_redirect;
    protected $_response;

    public function __construct(
        RedirectInterface $redirect,
        ResponseInterface $response 
    ){
        $this->_redirect = $redirect;
        $this->_response = $response;
    }
    public function execute(Observer $observer) {
        $myfile = fopen("/var/www/html/magento/var/logdebug.log", "a+") or die("Unable to open file!");
        $abc = "heello";
        fwrite($myfile, print_r($abc,true));
        fclose($myfile);
        $this->_redirect->redirect($this->_response, '*/auth/login');
    }
}
12
  • hey __construct seems broken can you check
    – mcoder
    Commented Apr 18, 2016 at 7:52
  • seems not working.........
    – mcoder
    Commented Apr 18, 2016 at 8:13
  • check the edited ans Commented Apr 18, 2016 at 8:16
  • hey worked but issue it that add to cart is done via ajax when i inspect console i see it was calling auth/login , but did not redirected to that url
    – mcoder
    Commented Apr 18, 2016 at 8:21
  • bro can you help how to fix it
    – mcoder
    Commented Apr 18, 2016 at 8:37

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.