How to Get New Products Collection

New products can be considered the lifeblood of any firm, especially online businesses. Without them, the online store will face its decline stage, which can result in being acquired by other stores.

In case you don’t know, getting a product collection in Magento 2 simply means showing the items in your Magento 2 store when you run the command.

However, you might need the data about new products collection to display new products. Therefore, in today article, I will guide you on how to get New Products Collection in Magento 2.

Let’s start!

3 Steps 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

And that’s all you can do to get new product collections in Magento 2. I hope that this tutorial is helpful for you. After reading this, you will be able to display your collection easily.

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