2

I am new to Magento 2 and I can not find view.phtml file in Catalog. I want to show previous and next product from the category of the current product in Product detail Page, do any on know how can I do this?

3

This case, you should create a custom module which have helper class.

At this helper class,create two functions getPreviousProduct() and getNextProduct() which give us previous and next product link receptively .

Helper class:

<?php
namespace Company1\Module1\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_coreRegistry;   
    protected $productRepository; 
     public function __construct(
            \Magento\Framework\Registry $coreRegistry,
            \Magento\Catalog\Api\ProductRepositoryInterface $productRepository

         )
    {
        $this->_coreRegistry = $coreRegistry;
        $this->productRepository = $productRepository;
    }
  public function getNextProduct()
    {
        $prodId = $this->_coreRegistry->registry('current_product')->getId();

        $catArray = $this->_coreRegistry->registry('current_category');

        if($catArray){
            $catArray = $catArray->getProductsPosition();
            $keys = array_flip(array_keys($catArray));
            $values = array_keys($catArray);

            $productId = $values[$keys[$prodId]-1];

            $product = $this->productRepository->getById($productId);

            if($product->getId()){
               return $product->getProductUrl();
            }
            return false;
        }

        return false;
    }
    public function getPreviousProduct()
    {
        $prodId = $this->_coreRegistry->registry('current_product')->getId();

        $catArray = $this->_coreRegistry->registry('current_category');

        if($catArray){
            $catArray = $catArray->getProductsPosition();
            $keys = array_flip(array_keys($catArray));
            $values = array_keys($catArray);

            $productId = $values[$keys[$prodId]-1];

            $product = $this->productRepository->getById($productId);

            $product = $this->productRepository->getById($productId);

            if($product->getId()){
               return $product->getProductUrl();
            }
            return false;
        }

        return false;

    }
}

Then you need call this helper class review summary phtml file app/design/frontend/Vendor/template/Magento_Catalog/tamplates/product/view/review.phtml

call add this code

<a href="<?php $this->helper('Company1\Module1\Helper\Data')->getPreviousProduct() ?>">Previouse</a>

<a href="<?php $this->helper('Company1\Module1\Helper\Data')->getNextProduct() ?>">Next</a>

Note: Code is not tested, is created basic of idea

  • $this->_coreRegistry->registry('current_category'); will not work once we are into detail page from search results – jafar pinjar Oct 3 '18 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.