Magento – How to redirect on specific product page in magento2

frontendmagento2product

I want to redirect on specific product page when i click on detail view link.

For ex. :- In grid , I click on first product detail. After I want to redirect on that specific product view page.

How to do it..?

In this image d1 is product sku . I want to do it when I clicked on view then it will redirect on d1 product view page .

enter image description here

Best Answer

May be you want product Url by using sku

==================

<?php
namespace Company1\Module1\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_productRepository;
    public function __construct(
         \Magento\Framework\App\Helper\Context$context
        \Magento\Catalog\Model\ProductRepository $productRepository,
    )
    {
        $this->_productRepository = $productRepository;
        parent::__construct($context);
    } 
    public function getProductBySku($sku)
    {
        $_product = $this->_productRepository->get($sku);
        if(!$_product){
            return false;
        }
        return $_product->getProductUrl();

    }   

}

Then add at phtml

$_helper = $this->helper('Company1\Module1\Helper\Data');
$producturl = $_helper->getProductBySku([sku]); 

at view for getting product URL

Related Topic