/** * Product view action * * @return void */ 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->getRequest()->isPost() && $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) { $product = $this->_initProduct(); if (!$product) { $this->noProductRedirect(); } if ($specifyOptions) { $notice = $product->getTypeInstance()->getSpecifyOptionMessage(); $this->messageManager->addNotice($notice); } $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl()); return; } // Prepare helper and params /** @var \Magento\Catalog\Helper\Product\View $viewHelper */ $viewHelper = $this->_objectManager->get('Magento\\Catalog\\Helper\\Product\\View'); $params = new \Magento\Framework\Object(); $params->setCategoryId($categoryId); $params->setSpecifyOptions($specifyOptions); // Render page try { $viewHelper->prepareAndRender($productId, $this, $params); } catch (\Exception $e) { if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) { $this->noProductRedirect(); } else { $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e); $this->_forward('noroute'); } } }
/** * 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->getRequest()->isPost() && $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) { $product = $this->_initProduct(); if (!$product) { return $this->noProductRedirect(); } 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')->jsonEncode(['backUrl' => $this->_redirect->getRedirectUrl()])); return; } $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setRefererOrBaseUrl(); return $resultRedirect; } // Prepare helper and params $params = new \Magento\Framework\Object(); $params->setCategoryId($categoryId); $params->setSpecifyOptions($specifyOptions); // Render page try { $page = $this->resultPageFactory->create(false, ['isIsolated' => true]); $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')->critical($e); $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('noroute'); return $resultForward; } }