We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Create a new file nextpreviouspagination.phtml at app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/view/.
    Add following code in this new file:

    <?php 
    
        $productId = $block->getProduct()->getId(); 
        $categoryIds = $block->getProduct()->getCategoryIds();
    
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    
        $nxtProduct = $objectManager->create('MagentoCatalogModelProduct');
        $prvProduct = $objectManager->create('MagentoCatalogModelProduct');
    
        $category = $objectManager->create('MagentoCatalogModelCategory')->load($categoryIds[0]);
        $categoryProduct = $category->getProductCollection()->addAttributeToSort('position', 'asc');
        $categoryProduct->addAttributeToFilter('status',1);
        $categoryProduct->addAttributeToFilter('visibility',4);
    
        $categoryProductIds = $categoryProduct->getAllIds();
    
        $position = array_search($productId, $categoryProductIds); // get position of the current product
        $nextPosition = $position+1;
        $previousPosition = $position-1;
        $keys = array_keys($categoryProductIds);
    
        if(in_array($nextPosition, $keys)){
            $nextProduct = $nxtProduct->load($categoryProductIds[$nextPosition]);
        }
        if(in_array($previousPosition, $keys)){
            $previousProduct = $prvProduct->load($categoryProductIds[$previousPosition]);   
        }
    
    ?>
    
    <div class="previous_next">
        <?php if(in_array($previousPosition, $keys)): ?>
            <a href="<?php print_r($previousProduct->getProductUrl()); ?>"><span>Previous </span></a>
        <?php endif; ?>
    
        <?php if(in_array($nextPosition, $keys)): ?>
            <a href="<?php print_r($nextProduct->getProductUrl()); ?>"><span>Next </span></a>
        <?php endif; ?>
    </div>

    Now open this file app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml.
    Add following code between body tag of the catalog_product_view.xml file:

    <referenceContainer name="content">
        <block class="MagentoCatalogBlockProductViewDescription" name="product.info.nextpreviouspagination" template="product/view/nextpreviouspagination.phtml" />   
    </referenceContainer>
    
    <move element="product.info.nextpreviouspagination" destination="product.info.main" after="page.main.title"/>

     

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote