Magento 2 – How to Get Next and Previous Product on Detail Page

magento2

How to navigate NEXT and PREVIOUS products in product detail page.
I tried with below code, it's showing the NEXT and PREVIOUS product's link but it's not changing the product.

Please do the needful.

I refered this code from arun

function getNextProductUrl($category, $currentProduct) {

    $productPositions = $category->getProductsPosition();
    asort($productPositions);
    $sortedCatProductIds = array_keys($productPositions);

    $currentProductCategoryIndex = @array_search($currentProduct->getId(), $sortedCatProductIds);

    $nextProductIds = array_slice($sortedCatProductIds, $currentProductCategoryIndex + 1, count($sortedCatProductIds)); //+1 for next product, -1 for previous product

    foreach ($nextProductIds as $productId) {
        $product = Mage::getModel('catalog/product')->load($productId);

        if ($product && $product->getId() && $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility()) {
            return $product->getProductUrl();
        }
    }

    return false;
}  

This is how I am calling the url.

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

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

Best Answer

Create following file

app/design/frontend/Magento/your_theme/Magento_Catalog/templates/product/view/previousnext.phtml

write below code in previousnext.phtml file

<?php 

    $productId = $block->getProduct()->getId(); 
    $cat_ids = $block->getProduct()->getCategoryIds();

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    $product = $objectManager->create('Magento\Catalog\Model\Product');
    $productt = $objectManager->create('Magento\Catalog\Model\Product');

    $category = $objectManager->create('Magento\Catalog\Model\Category')->load($cat_ids[0]);
    // print_r($category->getUrl());
    $category_product = $category->getProductCollection()->addAttributeToSort('position', 'asc');
    $category_product->addAttributeToFilter('status',1);
    $category_product->addAttributeToFilter('visibility',4);

    $cat_prod_ids = $category_product->getAllIds();

    $_pos = array_search($productId, $cat_prod_ids); // get position of current product
    $_next_pos = $_pos+1;
    $_prev_pos = $_pos-1;
    $keys = array_keys($cat_prod_ids);

    if(in_array($_next_pos, $keys)){
        $_next_prod = $product->load($cat_prod_ids[$_next_pos]);
    }
    if(in_array($_prev_pos, $keys)){
        $_prev_prod = $productt->load($cat_prod_ids[$_prev_pos]);   
    }

?>

<div class="previous_next">
    <?php if(in_array($_prev_pos, $keys)): ?>
        <a href="<?php print_r($_prev_prod->getProductUrl()); ?>"><span>PREVIOUS </span></a>
    <?php endif; ?>

    <?php if(in_array($_next_pos, $keys)): ?>
        <a href="<?php print_r($_next_prod->getProductUrl()); ?>"><span>NEXT </span></a>
    <?php endif; ?>
</div> 

Now navigate to

app/design/frontend/Magento/your_theme/Magento_Catalog/layout/catalog_product_view.xml

write below code between body tag of catalog_product_view.xml file

<referenceContainer name="content">
    <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.previousnext" template="product/view/previousnext.phtml" />   
</referenceContainer>

<move element="product.info.previousnext" destination="product.info.main" after="page.main.title"/>