0

I have successfully listed the best selling products, but how should I give it a link to open the same product in the product detail page whenever we click on it. The same product should open at the detail page which is clicked. Below is my code..

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Reports\Model\ResourceModel\Report\Collection\Factory'); 
$collection = $productCollection->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection');
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue("web/unsecure/base_url");
// echo $conf;

$collection->setPeriod('month');
//$collection->setPeriod('year');
//$collection->setPeriod('day');
foreach ($collection as $item) { ?>
    <ol>
    <li><a href=""><?php $productId = $item->getProductId(); //Product Id

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
            $image = $conf."pub/media/catalog/product/".$product->getImage();
            echo "<img src='$image'>" ?></a></li>
    <li><?php echo $item->getProductName(); ?></li>
    <li><?php echo $item->getProductPrice(); ?></li>
    </ol>
<?php }

?>

1 Answer 1

0

You can use below code to get product URL in loop.

$product->getProductUrl()

Example:

<a href="<?php echo $product->getProductUrl();?>"><?php echo $image;?></a>

Your Answer

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

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