I override the controller Product/view.php
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magenticians\AddModule\Controller\Rewrite\Product;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class View extends \Magento\Catalog\Controller\Product
{
/**
* @var \Magento\Catalog\Helper\Product\View
*/
protected $viewHelper;
/**
* @var \Magento\Framework\Controller\Result\ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
protected $_categoryFactory;
protected $_productFactory;
Protected $customerSession;
/**
* Constructor
*
* @param Context $context
* @param \Magento\Catalog\Helper\Product\View $viewHelper
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
\Magento\Catalog\Helper\Product\View $viewHelper,
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
\Magento\Customer\Model\Session $customerSession,
PageFactory $resultPageFactory,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Catalog\Model\ResourceModel\CategoryFactory $categoryFactory
)
{
$this->viewHelper = $viewHelper;
$this->resultForwardFactory = $resultForwardFactory;
$this->resultPageFactory = $resultPageFactory;
$this->_categoryFactory = $categoryFactory;
$this->_productFactory = $productFactory;
$this->customerSession = $customerSession;
parent::__construct($context);
}
/**
* Redirect if product failed to load
*
* @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\Controller\Result\Forward
*/
protected function noProductRedirect()
{
$store = $this->getRequest()->getQuery('store');
if (isset($store) && !$this->getResponse()->isRedirect()) {
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('');
} elseif (!$this->getResponse()->isRedirect()) {
$resultForward = $this->resultForwardFactory->create();
$resultForward->forward('noroute');
return $resultForward;
}
}
/**
* Product view action
*
* @return \Magento\Framework\Controller\Result\Forward|\Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
// Get initial data from request
$categoryId = (int)$this->getRequest()->getParam('category', false);
$productId = (int)$this->getRequest()->getParam('id');
$specifyOptions = $this->getRequest()->getParam('options');
if (!$this->_request->getParam('___from_store')
&& $this->_request->isPost()
&& $this->_request->getParam(self::PARAM_NAME_URL_ENCODED)) {
$product = $this->_initProduct();
$cats = $product->getCategoryIds(); // All Categories of $product you will get in array format eg. array(catid 1, catid 2, catid 3
if (!$product) {
return $this->noProductRedirect();
}else if ($product && (!($this->customerSession->isLoggedin())) && ($cats == 8)) {
die ("ggggggggggggggggg44444444444");
$this->customerSession->setAfterAuthUrl($this->_url->getCurrentUrl());
$this->customerSession->authenticate();
die ("ggggggggggggggggg");
}
if ($specifyOptions) {
$notice = $product->getTypeInstance()->getSpecifyOptionMessage();
$this->messageManager->addNotice($notice);
}
if ($this->getRequest()->isAjax()) {
$this->getResponse()->representJson(
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode([
'backUrl' => $this->_redirect->getRedirectUrl()
])
);
return;
}
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setRefererOrBaseUrl();
return $resultRedirect;
}
// Prepare helper and params
$params = new \Magento\Framework\DataObject();
$params->setCategoryId($categoryId);
$params->setSpecifyOptions($specifyOptions);
// Render page
try {
$page = $this->resultPageFactory->create();
$this->viewHelper->prepareAndRender($page, $productId, $this, $params);
return $page;
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
return $this->noProductRedirect();
} catch (\Exception $e) {
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$resultForward = $this->resultForwardFactory->create();
$resultForward->forward('noroute');
return $resultForward;
}
}
}
Below the di.xml
file code.
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Controller\Category\View" type="Magenticians\AddModule\Controller\Rewrite\Category\View" />
<preference for="Magento\Catalog\Controller\Product\View" type="Magenticians\AddModule\Controller\Rewrite\Product\View" />
</config>
But even when i try with var_dump there is no result can any one tell me the error in my code.