I have written a new code from scratch for one of my old extension from Magento 1.9 in Magento 2. Everything is working fine except the couple of functionality for custom type product forward to other site.
1. The redirect controller is not forwarding the page other website once I called it from Add to cart from view page.
2. The redirect will read the properties from Admin configuration for Authentication either user required login or not before forwarding. I have configured it in admin but not sure how to read this in controller or model.
Following is the code of Controller:
<?php namespace OfferDeal\AffiliateProduct\Controller; class Redirect extends \Magento\Framework\App\Action\Action { /** @var \OfferDeal\AffiliateProduct\Helper\Data */ protected $helper; /** * @var \OfferDeal\AffiliateProduct\Helper\Data */ protected $affiliateProductHelper; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * @var \Magento\Customer\Model\Session */ protected $customerSession; /** * @var \Magento\Framework\Session\Generic */ protected $generic; /** * @var \Magento\Catalog\Model\ProductFactory */ 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 ) { $this->affiliateProductHelper = $affiliateProductHelper; $this->scopeConfig = $scopeConfig; $this->customerSession = $customerSession; $this->generic = $generic; $this->catalogProductFactory = $catalogProductFactory; parent::__construct( $context ); } /** * Protected construct method * * @return void */ protected function _construct() { parent::_construct(); $this->helper = $this->affiliateProductHelper; } /** * Make sure the customer is authenticated of necessary * * @return \Magento\Framework\App\Action\Action | void */ public function preDispatch() { parent::preDispatch(); if (!$this->getRequest()->isDispatched()) { return; } $authenticationRequired = (bool) $this->scopeConfig->getValue( \OfferDeal\AffiliateProduct\Model\Product\Type::XML_PATH_AUTHENTICATION , \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if ($authenticationRequired) { $customer = $this->customerSession->getCustomer(); if ($customer && $customer->getId()) { $validationResult = $customer->validate(); if ((true !== $validationResult) && is_array($validationResult)) { foreach ($validationResult as $error) { $this->generic->addError($error); } $this->goBack(); $this->setFlag('', self::FLAG_NO_DISPATCH, true); return $this; } return $this; } else { $this->customerSession->addError( $this->helper->__('You must log in to access the partner product') ); $this->_redirect('customer/account/login'); $this->setFlag('', self::FLAG_NO_DISPATCH, true); return $this; } } } /** * Redirect to partner link * * @return void */ public function productAction() { $productId = (int) $this->getRequest()->getParam('id'); $product = $this->catalogProductFactory->create()->load($productId); if (($product instanceof \Magento\Catalog\Model\Product) && ($product->getTypeId() === \OfferDeal\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; } $this->getResponse()->setRedirect($product->getAffiliateLink()); return; } else { $this->generic->addError( $this->helper->__('Affiliate product not found') ); $this->goBack(); return; } } /** * Performs a redirect to a previously visited page * * @return \OfferDeal\AffiliateProduct\Controller\Redirect */ protected function goBack() { $returnUrl = $this->_getRefererUrl(); if ($returnUrl) { $this->getResponse()->setRedirect($returnUrl); } else { $this->_redirect('checkout/cart'); } return $this; } /** * @override * @see \Magento\Framework\App\Action\Action::execute() * @return \Magento\Framework\Controller\Result\Redirect */ public function execute() { return $this->$catalogProductFactory->create(); } }
Following is the phtml code for Add to cart
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ // @codingStandardsIgnoreFile /** @var $block \Magento\Catalog\Block\Product\View */ ?> <?php $_product = $block->getProduct(); ?> <?php $buttonTitle = __('Go to partner site'); ?> <?php if ($_product->isSaleable()): ?> <div class="box-tocart"> <div class="fieldset"> <div class="actions"> <button class="action primary tocart" class="action primary tocart" onClick="window.open('<?php echo $_product->getAffiliateLink(); ?>')"> <span><span><?php echo $buttonTitle ?></span> <span class="fa fa-shopping-cart"></span></span> </button> <!-- <button type="submit" title="<?= /* @escapeNotVerified */ $buttonTitle ?>" class="action primary tocart" onclick="javascript:window.location.href='<?php /* echo Mage::helper('offerdeal_affiliateproduct')->getRedirectUrl($_product); */ ?>'; return false;" > <span><?= /* @escapeNotVerified */ $buttonTitle ?></span> </button> --> <?= $block->getChildHtml('', true) ?> </div> </div> <?php echo $_product->getTypeId(); ?> <?php echo $_product->getAffiliateLink(); ?> <?php /* echo $this->helper('OfferDeal\AffiliateProduct\Helper\Data')->getRedirectUrl($_product); */?> </div> <?php endif; ?>
Following is the code for Model:
<?php namespace OfferDeal\AffiliateProduct\Model\Product; class Type extends \Magento\Catalog\Model\Product\Type\Virtual { const TYPE_AFFILIATE = 'affiliate'; const XML_PATH_AUTHENTICATION = 'catalog/affiliate/authentication'; /** * Model Initialization * * @return void */ protected function _construct() { $this->_init('offerdeal_affiliateproduct', 'id'); } public function _prepareProduct(\Magento\Framework\DataObject $buyRequest, $product, $processMode) { if ($this->_isStrictProcessMode($processMode)) { return __( 'Affiliate product %s cannot be added to cart. ' . ' On the product detail page click the "Go to parent site" button to access the product.', $product->getName() ); } return parent::_prepareProduct($buyRequest, $product, $processMode); } /** * Delete data specific for Custom product type * * @param \Magento\Catalog\Model\Product $product * @return void */ public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product) { } }
<?php namespace OfferDeal\AffiliateProduct\Model\Config\Source; class Authentication implements \Magento\Framework\Option\ArrayInterface { /** * {@inheritdoc} * * @codeCoverageIgnore */ public function toOptionArray() { return [ ['value' => 'yes', 'label' => __('Yes')], ['value' => 'no', 'label' => __('No')] ]; } }
I have created an affliate_link product attribute to keep the link of other site and Cutom Affliate Product type. I can able to forward the page with help of affliate link but not sure how to redirect it with controller rather than attributes.
Can someone help me in figuring out what I did wrong?