viewHelper = $viewHelper; $this->resultForwardFactory = $resultForwardFactory; $this->resultPageFactory = $resultPageFactory; $this->logger = $logger ?: ObjectManager::getInstance() ->get(LoggerInterface::class); $this->jsonHelper = $jsonHelper ?: ObjectManager::getInstance() ->get(Data::class); $this->catalogDesign = $catalogDesign ?: ObjectManager::getInstance() ->get(Design::class); $this->productRepository = $productRepository ?: ObjectManager::getInstance() ->get(ProductRepositoryInterface::class); $this->storeManager = $storeManager ?: ObjectManager::getInstance() ->get(StoreManagerInterface::class); } /** * Redirect if product failed to load * * @return Redirect|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; } return $this->getResponse(); } /** * Product view action * * @return Forward|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->addNoticeMessage($notice); } if ($this->getRequest()->isAjax()) { $this->getResponse()->representJson( $this->jsonHelper->jsonEncode( [ 'backUrl' => $this->_redirect->getRedirectUrl() ] ) ); return; } $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setUrl($this->_url->getCurrentUrl()); return $resultRedirect; } // Prepare helper and params $params = new DataObject(); $params->setCategoryId($categoryId); $params->setSpecifyOptions($specifyOptions); // Render page try { $this->applyCustomDesign($productId); $page = $this->resultPageFactory->create(); $this->viewHelper->prepareAndRender($page, $productId, $this, $params); return $page; } catch (NoSuchEntityException $e) { return $this->noProductRedirect(); } catch (\Exception $e) { $this->logger->critical($e); $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('noroute'); return $resultForward; } } /** * Apply custom design from product design settings * * @param int $productId * @throws NoSuchEntityException */ private function applyCustomDesign(int $productId): void { $product = $this->productRepository->getById($productId, false, $this->storeManager->getStore()->getId()); $settings = $this->catalogDesign->getDesignSettings($product); if ($settings->getCustomDesign()) { $this->catalogDesign->applyCustomDesign($settings->getCustomDesign()); } } }