1

i tried to replicate : Redirect add to cart for affiliate products

in magento 2 but it does not work below is my module code

Devshree\Affliliate\etc\frontend\events.xml

code:

<?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="controller_action_predispatch_checkout_cart_add">
        <observer name="devshree_affliliate_event_obs" instance="Devshree\Affliliate\Observer\redirectExternalLink" />
    </event>
</config>

and Devshree\Affliliate\Model\Observer.php

code :

namespace Devshree\Affliliate\Observer;

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

class redirectExternalLink implements ObserverInterface {
    public function execute(Observer $observer) {
        $productId = Mage::app()->getRequest()->getParam('product', 0);
        $product = Mage::getModel('catalog/product')->load($productId);
        // Check for value set on this product for external_link attribute.
        if ($url = $product->getExternalLink()) {
            Mage::app()->getResponse()->setRedirect($url);
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
        return $this;
    }
}
1
  • You've copied a little too much from that Magento 1 snippet: there's no Mage::app() in Magento 2. Haven't tried, but I think you can do it by injecting \Magento\Framework\App\Response\Http in your observer, and setting the redirect URL in that, then exiting.
    – nevvermind
    Apr 18 '16 at 0:15
1

You have to use "ActionFlag" and set a NO_DISPATCH. Using exit is not recommended:

<?php
...
use \Magento\Framework\App\ActionFlag;
...
class MyObserver implements ObserverInterface
{
    ...
    protected $actionFlag;
    ...

    public function __construct(
        ...
        ActionFlag $actionFlag
    ) {
        ...
        $this->actionFlag = $actionFlag;
    }

    public function execute(Observer $observer)
    {
        $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        $controller->getResponse()->setRedirect(
            $controller->getUrl($myNewUrl)
        );
    }
    ...

P.S: Your code is Magento 1 mixed with Magento 2, you should use Magento 2 way.

0
1

Here are the following codes for checking the parameter/attribute for checking affiliate program.

<?php

namespace Test\Mymodule\Observer;

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

class TestObserver implements ObserverInterface
{
    protected $actionFlag;

    public function __construct(
        ActionFlag $actionFlag
    ) {
        $this->actionFlag = $actionFlag;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $productUrl = $currentProduct->getProductUrl()
        $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
        $controller->getResponse()->setRedirect(
            $controller->getUrl($productUrl)
        );

    }
}

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.