We are very exciting after we have successfully created featured product module and We want to share with you.
Let’s create the featured product module. We will need
app/code/Webmull/Featuredproduct/Block

app/code/Webmull/Featuredproduct/etc

app/code/Webmull/Featuredproduct/Setup

app/code/Webmull/Featuredproduct/view/frontend/templates

Now, We have created directory structure for the module. Now we will create the file for this.

1. First we create the configuration file module.xml in app/code/Webmull/Featuredproduct/etc

The content for the file will be as:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

<module name="Webmull_Featuredproduct" setup_version="2.0.0" />

</config>

 

2. we create the product attribute named InstallData.php in app/code/Webmull/Featuredproduct/Setup

The content for the file should be:

<?php


namespace Webmull\Featuredproduct\Setup;

 

use Magento\Eav\Setup\EavSetup;

use Magento\Eav\Setup\EavSetupFactory;

use Magento\Framework\Setup\InstallDataInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\ModuleDataSetupInterface;

 

class InstallData implements InstallDataInterface

{

/**

* EAV setup factory

*

* @var EavSetupFactory

*/

private $eavSetupFactory;

 

/**

* Init

*

* @param EavSetupFactory $eavSetupFactory

*/

public function __construct(EavSetupFactory $eavSetupFactory)

{

$this->eavSetupFactory = $eavSetupFactory;

}

 

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

{

/** @var EavSetup $eavSetup */

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

 

/**

* Add attributes to the eav/attribute

*/

$eavSetup->addAttribute(

\Magento\Catalog\Model\Product::ENTITY,

'is_featured',

[

'group' => 'General',

'type' => 'int',

'backend' => '',

'frontend' => '',

'label' => 'Featured Product',

'input' => 'boolean',

'class' => '',

'source' => '',

'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,

'visible' => true,

'required' => false,

'user_defined' => true,

'default' => '',

'searchable' => false,

'filterable' => false,

'comparable' => false,

'visible_on_front' => false,

'used_in_product_listing' => true,

'unique' => false,

'apply_to' => 'simple,configurable,virtual,bundle,downloadable'

]

);

 

}

}

 

3. Create the block file named Featuredproduct.php in app/code/Webmull/Featuredproduct/Block

 

namespace Webmull\Featuredproduct\Block;

use Magento\Customer\Model\Context as CustomerContext;

class Featuredproduct extends \Magento\Catalog\Block\Product\AbstractProduct 
{

    protected $_productcollection;
    
    /**
     * @var \Magento\Framework\Url\Helper\Data
     */
    protected $urlHelper;
    /**
     * Catalog product visibility
     *
     * @var \Magento\Catalog\Model\Product\Visibility
     */
    protected $_catalogProductVisibility;
    
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
	\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productcollection,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        array $data = []
    ) {
        $this->_productcollection = $productcollection;
        $this->urlHelper = $urlHelper;
        $this->_catalogProductVisibility = $catalogProductVisibility;
        parent::__construct($context, $data);
    }
    
    public function getFeaturedProduct(){ 
        $collection =  $this->_productcollection->create()
                        ->addAttributeToFilter('status', '1')
                        ->addAttributeToFilter('is_featured', '1');
        $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());	
	$collection = $this->_addProductAttributesAndPrices($collection)
                            ->setPageSize(4)
                            ->setCurPage(1);
		
        return $collection;
    }
    /**
     * Get post parameters
     *
     * @param \Magento\Catalog\Model\Product $product
     * @return string
     */
    public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product)
    {
        $url = $this->getAddToCartUrl($product);
        return [
            'action' => $url,
            'data' => [
                'product' => $product->getEntityId(),
                \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED =>
                    $this->urlHelper->getEncodedUrl($url),
            ]
        ];
    }
	
}

 

4. Create the featuredprodcut.phtml file in app/code/Webmull/Featuredproduct/view/frontend/templates

 

<h1><?php echo __('Featured Product'); ?></h1>
<?php 
$_productCollection = $block->getFeaturedProduct();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\Image');
?>
<?php 
$viewMode = 'grid';
$image = 'category_page_grid';
$showDescription = false;
$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;

/**
 * Position for actions regarding image size changing in vde if needed
 */
$pos = $block->getPositioned();
$position = '';
?>
<?php if ($_productCollection->count()): ?>
    <div class="products wrapper <?php echo $viewMode; ?> products-<?php echo $viewMode; ?>">
        <?php $iterator = 1; ?>
        <ol class="products list items product-items">
            <?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
            <?php foreach ($_productCollection as $_product): ?><?php //echo "<pre>"; print_r($_product->getData()); exit; ?>
                <?php echo($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
                <div class="product-item-info" data-container="product-grid">
                    <?php // Product Image ?>
                    <?php
                    $productImage = $block->getImage($_product, $image);
                    if ($pos != null) {
                        $position = ' style="left:' . $productImage->getWidth() . 'px;'
                            . 'top:' . $productImage->getHeight() . 'px;"';
                    }
                    ?>
                    <a href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                        <?php echo $productImage->toHtml(); ?>
                    </a>
                    <div class="product details product-item-details">
                        <?php
                        $_productNameStripped = $block->stripTags($_product->getName(), null, true);
                        ?>
                        <strong class="product name product-item-name">
                            <a class="product-item-link"
                               href="<?php /* @escapeNotVerified */ echo $_product->getProductUrl() ?>">
                                <?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                            </a>
                        </strong>
                        <?php echo $block->getReviewsSummaryHtml($_product, $templateType); ?>
                        <?php /* @escapeNotVerified */ echo $block->getProductPrice($_product) ?>
                        <?php echo $block->getProductDetailsHtml($_product); ?>
                        <div class="product-item-inner">
                            <div class="product actions product-item-actions"<?php echo strpos($pos, $viewMode . '-actions') ? $position : ''; ?>>
                                <div class="actions-primary"<?php echo strpos($pos, $viewMode . '-primary') ? $position : ''; ?>>
                                    <?php if ($_product->isSaleable()): ?>
                                        <?php $postParams = $block->getAddToCartPostParams($_product); ?><?php //echo "<pre>"; print_r($postParams); exit;?>
                                        <form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
                                            <input type="hidden" name="product" value="<?php echo $postParams['data']['product']; ?>">
                                            <input type="hidden" name="<?php echo \Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][\Magento\Framework\App\Action\Action::PARAM_NAME_URL_ENCODED]; ?>">
                                            <?php echo $block->getBlockHtml('formkey')?>
                                            <button type="submit"
                                                    title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                    class="action tocart primary">
                                                <span><?php echo __('Add to Cart') ?></span>
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <?php if ($_product->getIsSalable()): ?>
                                            <div class="stock available"><span><?php echo __('In stock') ?></span></div>
                                        <?php else: ?>
                                            <div class="stock unavailable"><span><?php echo __('Out of stock') ?></span></div>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </div>
                                <div data-role="add-to-links" class="actions-secondary"<?php echo strpos($pos, $viewMode . '-secondary') ? $position : ''; ?>>
                                    <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                        <a href="#" 
                                           class="action towishlist"
                                           title="<?php echo $block->escapeHtml(__('Add to Wishlist')); ?>"
                                           aria-label="<?php echo $block->escapeHtml(__('Add to Wishlist')); ?>"
                                           data-post='<?php echo $block->getAddToWishlistParams($_product); ?>'
                                           data-action="add-to-wishlist"
                                           role="button">
                                            <span><?php echo __('Add to Wishlist') ?></span>
                                        </a>
                                    <?php endif; ?>
                                    <?php
                                    $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
                                    ?>
                                    <a href="#" 
                                       class="action tocompare"
                                       title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                       aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                       data-post='<?php echo $compareHelper->getPostDataParams($_product); ?>'
                                       role="button">
                                        <span><?php echo __('Add to Compare') ?></span>
                                    </a>
                                </div>
                            </div>
                            <?php if ($showDescription): ?>
                                <div class="product description product-item-description">
                                    <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>"
                                       class="action more"><?php echo __('Learn More') ?></a>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
        </div>
        <?php echo($iterator == count($_productCollection) + 1) ? '</li>' : '' ?>
    <?php endforeach; ?>
    </ol>
    </div>
<?php endif; ?>

 

Now you think module is ready but how it displayed in front. Let’s we are displaying the featured product block in home page
In Cms page we will add

 

{{block class="Webmull\Featuredproduct\Block\Featuredproduct" name="feaured" template="list.phtml"}}

 

Now Refresh the home page. We have sucessfully created the featured product module.
Enjoy the module. :)