3

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?

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Arun Karnawat
  • 2,948
  • 7
  • 34
  • 62

2 Answers2

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

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
2

I have created an extension to have that feature. You can refer my extension: https://github.com/davidduong90/Next-previous-product

I hope it helps someone!

David Duong
  • 803
  • 1
  • 11
  • 37