1

I have written custom redirect controller to for affiliate site but it is not working.

Following is my code: Helper

namespace Test\AffiliateProduct\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper {

    public function __construct( \Magento\Framework\App\Helper\Context $context) {
        parent::__construct(
            $context
        );
    }

    public function getRedirectUrl(\Magento\Catalog\Model\Product $product) {
        return Mage::getUrl('affiliate/redirect/product', array('id' => $product->getId()));
    }
}

Controller

namespace Test\AffiliateProduct\Controller;

class Redirect extends \Magento\Framework\App\Action\Action {
    protected $affiliateProductHelper;
    protected $scopeConfig;
    protected $customerSession;
    protected $generic;
    protected $catalogProductFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \OfferDeal\AffiliateProduct\Helper\Data $affiliateProductHelper,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Session\Generic $generic,
        \Magento\Catalog\Model\ProductFactory $catalogProductFactory
    ) {
        parent::__construct(
            $context
        );
        $this->affiliateProductHelper = $affiliateProductHelper;
        $this->scopeConfig = $scopeConfig;
        $this->customerSession = $customerSession;
        $this->generic = $generic;
        $this->catalogProductFactory = $catalogProductFactory;

    }
    public function execute()
    {
        $productId  = (int) $this->getRequest()->getParam('id');

        $product = $this->catalogProductFactory->create()->load($productId);

        if (($product instanceof \Magento\Catalog\Model\Product)
            && ($product->getTypeId() === \Test\AffiliateProduct\Model\Product\Type::TYPE_AFFILIATE)
        ) {
            if (!\Zend_Uri::check($product->getAffiliateLink())) {
                $this->generic->addError(
                    $this->helper->__('The partner product is not accessible.')
                );

                $this->goBack();
                return;
            }

            return $this->resultRedirectFactory->create()->setUrl($product->getAffiliateLink());

            //$this->getResponse()->setRedirect($product->getAffiliateLink());
            //return;

        } else {
            $this->generic->addError(
                $this->helper->__('Affiliate product not found')
            );

            $this->goBack();
            return;
        }

    }
    protected function goBack()
    {
        $returnUrl = $this->_getRefererUrl();

        if ($returnUrl) {
            $this->getResponse()->setRedirect($returnUrl);
        } else {
            $this->_redirect('checkout/cart');
        }

        return $this;
    }

}

View Add to Cart phtml

<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Shop Now'); ?>
<?php if ($_product->isSaleable()): ?>
<?php $helper = $this->helper('Test\AffiliateProduct\Helper\Data'); ?>
<div class="box-tocart">
<div class="add-to-cart">
        <button type="button" class="button btn-cart" onclick="javascript:window.location.href='<?php
            echo helper->getRedirectUrl($_product);?>'; return false;">
            <span><span><?php echo $buttonTitle ?></span></span>
        </button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
</div>
<?php endif; ?>

Can someone help in figureing out what is wrong with for Magento 2 redirect controller?

1 Answer 1

0

Mage::getUrl()) is wrong code.

You should use

$this->_getUrl('affiliate/redirect/product', array('id' => $product->getId()))

IN helper

4
  • It is getting HTTP ERROR 500. I am not sure where and how to debug this.
    – Prabhakar
    Commented Feb 27, 2018 at 8:47
  • I have fix 500 error and now i can see the controller is redirecting to affiliate/redirect/product/id/4/ URL but the issue is 404 and not forwarded to the url.
    – Prabhakar
    Commented Feb 27, 2018 at 9:43
  • I have fixed this controller as it was not loading. Thanks for the help it works now.
    – Prabhakar
    Commented Feb 28, 2018 at 9:47
  • You welcome ...
    – Amit Bera
    Commented Feb 28, 2018 at 9:48

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.