How to Get New Products Collection

New products can be considered as the lifeblood of any firms, especially online businesses. Without them, the online store will face its decline stage, which can result in being acquired by other stores. However, to display new products, you might need the data about new products collection. Therefore, in today article, I will guide you on how to get New Products Collection in Magento 2.

How to Get New Products Collection

Step 1: Create NewProducts block

To get New Products Collection, firstly, you need to create a NewProducts block. To do that, follow the path Mageplaza/Productslider/Block/NewProducts.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;
use Zend_Db_Expr;
/**
 * Class NewProducts
 * @package Mageplaza\Productslider\Block
 */
class NewProducts extends AbstractSlider
{
    /**
     * @inheritdoc
     */
    public function getProductCollection()
    {
        $visibleProducts = $this->_catalogProductVisibility->getVisibleInCatalogIds();
        $collection = $this->_productCollectionFactory->create()->setVisibility($visibleProducts);
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addAttributeToFilter(
                'news_from_date',
                ['date' => true, 'to' => $this->getEndOfDayDate()],
                'left'
            )
            ->addAttributeToFilter(
                'news_to_date',
                [
                    'or' => [
                        0 => ['date' => true, 'from' => $this->getStartOfDayDate()],
                        1 => ['is' => new Zend_Db_Expr('null')],
                    ]
                ],
                'left'
            )
            ->addAttributeToSort(
                'news_from_date',
                'desc'
            )
            ->addStoreFilter($this->getStoreId())
            ->setPageSize($this->getProductsCount());
        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

Then, 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, let’s flush cache and test result.

Conclusion

Above are three steps to help you get New Products Collection in Magento 2. I hope after reading this, you will be able to display your collection easily.

Enjoyed the tutorial? Spread it to your friends!