/** * Retrieve Visitor/Customer Last Viewed URL * * @return string */ public function getLastViewedUrl() { $productId = $this->_catalogSession->getLastViewedProductId(); if ($productId) { try { $product = $this->productRepository->getById($productId); } catch (NoSuchEntityException $e) { return ''; } /* @var $product \Magento\Catalog\Model\Product */ if ($this->_catalogProduct->canShow($product, 'catalog')) { return $product->getProductUrl(); } } $categoryId = $this->_catalogSession->getLastViewedCategoryId(); if ($categoryId) { try { $category = $this->categoryRepository->get($categoryId); } catch (NoSuchEntityException $e) { return ''; } /* @var $category \Magento\Catalog\Model\Category */ if (!$this->_catalogCategory->canShow($category)) { return ''; } return $category->getCategoryUrl(); } return ''; }
/** * @magentoDataFixture Magento/Catalog/_files/products.php */ public function testCanShow() { // non-visible or disabled /** @var $product \Magento\Catalog\Model\Product */ $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $this->assertFalse($this->_helper->canShow($product)); // enabled and visible $product->setId(1); $product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED); $product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); $this->assertTrue($this->_helper->canShow($product)); $this->assertTrue($this->_helper->canShow(1)); }
/** * Retrieve Visitor/Customer Last Viewed URL * * @return string */ public function getLastViewedUrl() { $productId = $this->_catalogSession->getLastViewedProductId(); if ($productId) { $product = $this->_productFactory->create()->load($productId); /* @var $product \Magento\Catalog\Model\Product */ if ($this->_catalogProduct->canShow($product, 'catalog')) { return $product->getProductUrl(); } } $categoryId = $this->_catalogSession->getLastViewedCategoryId(); if ($categoryId) { $category = $this->_categoryFactory->create()->load($categoryId); /* @var $category \Magento\Catalog\Model\Category */ if (!$this->_catalogCategory->canShow($category)) { return ''; } return $category->getCategoryUrl(); } return ''; }