How to Get Featured Product Collection in Magento 2?

Display featured products on your Magento store is an effective way to cross-sell and up-sell your products.

However, to display featured products, store admin need to get the featured product collection that might be not easy for new developers or non-technical store admins. Therefore, in today’s post, I will show you three steps to get Featured Product collection in Magento 2.


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


3 Steps to get Featured Product collection in Magento 2

Step 1: Create FeaturedProducts block

To get Feature Product collection, firstly, you need to create a FeaturedProducts block. To do that, follow the path Mageplaza/Productslider/Block/FeaturedProducts.php and add the below code:

<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Productslider
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
namespace Mageplaza\Productslider\Block;
/**
 * Class FeaturedProducts
 * @package Mageplaza\Productslider\Block
 */
class FeaturedProducts extends AbstractSlider
{
    /**
     * get collection of feature products
     * @return mixed
     */
    public function getProductCollection()
    {
        $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
        $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
        $collection->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addAttributeToSelect('*')
            ->addStoreFilter($this->getStoreId())
            ->setPageSize($this->getProductsCount())
            ->addAttributeToFilter('is_featured', '1');
        return $collection;
    }
}

Step 2: Insert in phtml file

After having the colletion in the block, now you can follow this snippet to get product colletion from the block Mageplaza/HelloWorld/view/frontend/templates/list.phtml.

Next, please insert the following code in the phtml file

<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

Step 3: Flush Cache & Test result

Finally, to finish getting Featured Product collection, let’s flush cache and test result.

Conclusion

Above is the instruction on how to get Featured Product collection in Magento 2. I hope this post is helpful for you when you want to get and display featured product collection on your store.

Thanks for reading!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x