Magento 2.3 get product colection

please read first post Magento 2.3 create simple module to make configure opitons for theme.

Make block get product colection

Create file app/code/<vendor_name>/<module_name>/Block/Products.php should look like:

<?php
namespace Magebay\Themes\Block;
use Magento\Framework\View\Element\Template;

class Products extends Template
{
	public function __construct(
		\Magento\Catalog\Block\Product\Context $context,
        \Magento\Catalog\Model\ProductFactory $productFactory,
		\Magento\Catalog\Model\Product\Visibility $visibility,
		\Magento\Catalog\Block\Product\ListProduct $abstractProduct,
        array $data = []
    ) {
        $this->_productModelFactory = $productFactory;
		$this->_productVisible = $visibility;
        parent::__construct($context, $data);
		$this->absblock = $abstractProduct;
    }
	public function getBestSellerProducts($categoryID = null)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();    
		$collection = $objectManager->get('\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory')->create()->setModel('Magento\Catalog\Model\Product');
        $producIds = array();
        foreach ($collection as $product) {
            $producIds[] = $product->getProductId();
        }
		$model = $this->_productModelFactory->create();
        $storeId = $model->getStoreId();
        $collection = $model->getCollection();
        $collection->setVisibility($this->_productVisible->getVisibleInCatalogIds());
        $collection->setStoreId($storeId)
            ->addAttributeToSelect('*')
            ->addStoreFilter($storeId);

        $collection = $collection->addStoreFilter()->addAttributeToFilter('entity_id', array('in' => $producIds));
		if($categoryID &amp;&amp; $categoryID != 0){
            /**
             * @var \Magento\Catalog\Model\Category $catModel
             */
            $catModel = $this->_categoryFactory->create();
            $catCollection = $catModel->load($categoryID);
            $collection->addCategoryFilter($catCollection);
        }
		$collection->setPageSize(10);
		return $collection;
    }
}

We’ll get BestSeller products on home page. So go to backend then paste below code to content home page.

{{block class="Magebay\Themes\Block\Products" name="bestseller.colection" template="Magebay_Themes::product/bestseller.phtml"}}

After create file <theme_dir>/Magento_Theme/templates/product/bestseller.phtml should look like:

<?php
$_productCollection = $block->getBestSellerProducts();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
?>
<?php if ($_productCollection->count()): ?>
	<div class="products wrapper grid products-grid">
        <ol class="products list items product-items">
            <?php foreach ($_productCollection as $_product): ?>
            <li class="item product product-item">
                <div class="product-item-info" data-container="product-grid">
                    <a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
                        <?php echo $block->absblock->getImage($_product,'category_page_grid')->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="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>">
                                <?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
                            </a>
                        </strong>
                        <?= $block->absblock->getReviewsSummaryHtml($_product,'short',true) ?>
                        <?= /* @escapeNotVerified */ $block->absblock->getProductPrice($_product) ?>
                        <div class="product-item-inner">
                            <div class="product actions product-item-actions">
                                <div class="actions-primary">
                                    <?php if ($_product->isSaleable()): ?>
                                        <?php $postParams = $block->absblock->getAddToCartPostParams($_product); ?>
                                        <form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" action="<?= /* @NoEscape */ $postParams['action'] ?>" method="post">
                                            <input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $postParams['data']['product'] ?>">
                                            <?= $block->absblock->getBlockHtml('formkey') ?>
                                            <button type="submit"
                                                    title="<?= __('Add to Cart') ?>"
                                                    class="action tocart primary">
                                                <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <?php if ($_product->isAvailable()): ?>
                                            <div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
                                        <?php else: ?>
                                            <div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </div>
                                <div data-role="add-to-links" class="actions-secondary">
                                    <?php if ($addToBlock = $block->absblock->getChildBlock('addto')): ?>
                                        <?= $addToBlock->setProduct($_product)->getChildHtml() ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </li>
            <?php endforeach; ?>
        </ol>
    </div>
<?php endif; ?>

We was update our file structure looks as follows:

app/code/Magebay/
├── Themes/
│ ├── Block/
│ │ ├── Products.php
app/design/frontend/Magebay/
├── Default/
│ ├── Magebay_themes/
│ │ ├── templates/
│ │ │ ├── product/
│ │ │ │ ├── bestseller.phtml

For next post we’ll add custom category attributes.