0

How to display best selling products on the home page?

0

Please Find Below answer for the best selling items.Copy code and paste into the template file and call template file to CMS Page.

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productCollectionFactory = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection');
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
    $storeId = $storeManager->getStore()->getId();
    $collection = $productCollectionFactory->addAttributeToSelect('*');
    $collection->getSelect()->joinLeft( 
                            'sales_order_item', 
                            'e.entity_id = sales_order_item.product_id', 
                            array('qty_ordered'=>'SUM(sales_order_item.qty_ordered)')) 
                            ->group('e.entity_id')->limit(8);
    $imageHelper = $objectManager->create('Magento\Catalog\Helper\Image');
    $_helper = $objectManager->create('Magento\Catalog\Helper\Output');
    $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');
?>

<div class="desktop-view-bestseller">
    <div class="block widget block-bestseller-products">
    <div class="container">
    <div class="row">
        <?php
            $viewMode = 'grid';
            $image = 'category_page_grid';
            $showDescription = false;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;


        ?>
        <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 ($collection 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" id="hover-product-<?php echo $_product->getId();?>" data-container="product-grid">
                        <?php // Product Image   ?>
                        <a href="<?php echo $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                            <span class="product-image-container">
                                <span class="product-image-wrapper">
                                    <?php
                                    $imageUrl = $imageHelper->init($_product, 'product_page_image_large')
                                                ->keepAspectRatio(TRUE)
                                                ->keepFrame(FALSE)
                                                ->resize(285, 285)
                                                ->getUrl();
                                    ?>
                                    <img class="product-image-photo" src="<?php echo $imageUrl; ?>" />
                                </span>
                            </span>
                        </a>

                        <div class="product details product-item-details">

                            <strong class="product name product-item-name">
                                <a class="product-item-link"
                                   href="<?php echo $_product->getProductUrl() ?>">
                                    <?php echo $_product->getName(); ?>   
                                </a>
                            </strong>
                            <div class="price-box price-final_price" data-role="priceBox" data-product-id="<?php echo $_product->getId(); ?>">
                                <span class="price-container price-final_price tax weee">
                                    <span id="old-price-<?php echo $_product->getId(); ?>-widget-product-grid" data-price-amount="<?php echo $_product->getPrice(); ?>" data-price-type="finalPrice" class="price-wrapper ">
                                        <span class="price"><?php echo $priceHelper->currency($_product->getPrice(), true, false); ?></span>
                                    </span>
                                </span>
                            </div>


                            <div class="product-item-inner">
                                <div class="product actions product-item-actions">
                                    <div class="actions-primary">
                                        <?php if ($_product->isSaleable()): ?>
                                            <?php
                                                $cartHelper = $objectManager->create('Magento\Checkout\Helper\Cart');
                                                $formKey = $objectManager->get('Magento\Framework\Data\Form\FormKey'); 
                                                $wishlistHelper = $objectManager->create('Magento\Wishlist\Helper\Data');
                                                $wishlistUrl = $wishlistHelper->getAddParams($_product);    

                                            ?>
                                            <form data-role="tocart-form" action="<?php echo $cartHelper->getAddUrl($_product); ?>" method="post">
                                                <input type="hidden" name="product" value="<?php echo $_product->getId(); ?>" />
                                                <input name="form_key" type="hidden" value="<?php echo $formKey->getFormKey();?>">
                                                <button type="submit"
                                                        title="<?php echo '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">
                                        <a href="#"
                                               class="action towishlist"
                                               title="<?php echo 'Add to Wish List'; ?>"
                                               aria-label="<?php echo 'Add to Wish List'; ?>"
                                               data-post='<?php echo $wishlistUrl; ?>'
                                               data-action="add-to-wishlist"
                                               role="button">
                                                <span><?php echo __('Add to Wish List') ?></span>
                                            </a>
                                    </div>

                                </div>
                            </div>


                        </div>
                    </div>
                    <?php echo($iterator == count($collection) + 1) ? '</li>' : '' ?>
                <?php endforeach; ?>
            </ol>
        </div>
        </div>
    </div>
    </div>
</div>

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