0

I have attempted to get the below helper class to function properly when called, however it returns the current product link rather than the previous and next links as it's intended. Also what would the proper way to filter products marked not visible. Any help is appreciated.

Helper Class

<?php
namespace Name\Module\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;

    }
}

Call within breadcrumbs.phtml

<?php if ($this->getRequest()->getFullActionName() == 'catalog_product_view') : ?>
    <a href="<?php $this->helper('Name\Module\Helper\Data')->getPreviousProduct() ?>">Previous</a>
    <a href="<?php $this->helper('Name\Module\Helper\Data')->getNextProduct() ?>">Next</a>
<?php endif; ?>

1 Answer 1

0

it seems like your code executing return false; even if your condition is true. Please add it in else statement.

<?php
namespace Name\Module\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();
            }else{
                return false;    
            }
        }else{
            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();
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

}
2
  • Same issue occurs. Aug 11, 2017 at 14:52
  • did you check your $prodId (current product id) and $productId are different??? Aug 11, 2017 at 14:53

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.