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?

2 Answers 2

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

1
  • $this->_coreRegistry->registry('current_category'); will not work once we are into detail page from search results Oct 3, 2018 at 11:50
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!

1
  • Awesome, Thank you!
    – MichaelHa
    Dec 12, 2019 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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