Magento 2: How to Check Product is New or Not

As we all know, there is a feature in Magento1 to set a product as new for a date duration. This feature is available in Magento 2 also.

However, I could not find a single place in Magento 2 to check whether a product is new or not. The below class will do the job for you. In the given example, I used the ViewModel concept in Magento2. However, you can use it as a helper in case you prefer it. So here we go.

<?php

namespace Namespace\Module\ViewModel;

use Magento\Catalog\Model\Product;
use Magento\Framework\View\Element\Block\ArgumentInterface;

class IsProductNew implements ArgumentInterface
{

    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_date;

    /**
     * IsProductNew constructor.
     *
     * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
     */
    public function __construct(\Magento\Framework\Stdlib\DateTime\DateTime $dateTime)
    {
        $this->_date = $dateTime;
    }

    /**
     * Check whether the product is New
     *
     * @return bool
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function isNew()
    {
        $fromDate = $this->_product->getNewsFromDate();
        $toDate   = $this->_product->getNewsToDate();

        if (!$fromDate || !$toDate) {
            return false;
        }

        $currentTime = $this->_date->gmtDate();
        $fromDate    = $this->_date->gmtDate(null, $fromDate);
        $toDate      = $this->_date->gmtDate(null, $toDate);

        $currentDateGreaterThanFromDate = (bool)($fromDate < $currentTime);
        $currentDateLessThanToDate      = (bool)($toDate > $currentTime);

        if ($currentDateGreaterThanFromDate && $currentDateLessThanToDate) {
            return true;
        }

        return false;
    }

    /**
     * Set a product for checking.
     *
     * @param \Magento\Catalog\Model\Product $product
     * @return $this
     */
    public function setProduct(Product $product)
    {
        $this->_product = $product;
        return $this;
    }
}

Here my module is Namespace_Module and the class IsProductNew has a public method setProduct() to pass the product to the class in order to check whether it is a new product or not. The main method in this class is isNew(). As you can see here, we are collecting the attribute values news_from_date and news_to_date. If they are set, then that means the product is featured as a new product from the backend.

Now we need to check whether the current datetime lies in the duration set to the product. For this, we will first turn $currentTime, $fromDate and $toDate into \Magento\Framework\Stdlib\DateTime\DateTime instances and then apply comparison checking.

That’s it. If you know a much more elegant solution than this one, then please let me know.

Rajeev K Tomy

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top