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

5,595 thoughts on “Magento 2: How to Check Product is New or Not

  1. Definitive journal of drugs and therapeutics. Comprehensive side effect and adverse reaction information.
    ivermectin 1mg
    drug information and news for professionals and consumers. Read now.

  2. Comprehensive side effect and adverse reaction information. What side effects can this medication cause?
    https://canadianfast.online/# cat antibiotics without pet prescription
    drug information and news for professionals and consumers. Definitive journal of drugs and therapeutics.

  3. Yeezy
    Adidas UK
    Air Jordan 4
    Air Jordan 1
    Jordan 1
    Air Jordans
    Nike Air Jordans
    Jordans 1
    Air Jordan 1
    Nike Jordan 1
    Nike Jordan
    Jordan 1s
    Jordan 1
    Adidas Yeezy
    Adidas Yeezy
    Yeezy
    Adidas Yeezy
    Adidas Yeezy
    Yeezy Shoes
    Air Jordan 1
    Air Jordan 1
    Jordan 1
    Jordan 1
    Air Jordan 4
    Air Jordans
    Air Jordan 1
    Jordan 1
    Air Jordan Shoes
    Air Jordan
    Air Jordans
    Air Jordan 1
    Jordan 1
    Jordan 1
    Jordan 1
    Jordan 1
    Jordan 11
    Jordan 1s
    Air Jordan 4
    Jordan 4
    AirJordan 4
    Jordan 4
    Air Jordan 4
    Jordan 4
    Jordan
    Jordan 4 Military Black
    Jordan 4
    Jordan 4s
    Jordan Retro 4
    Jordan 11
    Jordans 4
    Jordan Shoes
    Jordans Shoes
    NFL Shop Official Online Store
    Nike Air Jordan
    Nike Air VaporMax
    Nike Jordan 1
    Nike Jordans
    Nike Outlet
    Nike Outlet Store
    Nike Outlet
    Nike Outlet Store
    Nike Outlet
    Nike Shoes
    Nike UK
    Off White
    Yeezy
    Yeezy 350
    Yeezy
    Yeezy 350
    Yeezy Boost 350
    Yeezy 350 V2
    Yeezy 450
    Yeezy 700
    Adidas yeezy
    Yeezy Foam Runner
    Yeezy
    Yeezy
    Yeezy Shoes
    Yeezy
    Yeezy Slides
    Yeezy Slides
    Yeezy Supply
    Jordan Shoes
    Jordans
    Yeezy Slides
    Nike Shoes
    Nike Outlet
    Nike Outlet
    Ray-Ban Sunglasses
    Yeezy Shoes
    Moncler Jacket
    Moncler Outlet
    Moncler Outlet
    Moncler Jacket
    Moncler
    Moncler UK
    Moncler Jacket
    Moncler
    Moncler Outlet
    Yeezy
    Yeezy Shoes
    Yeezy Sneakers
    Yeezy
    Yeezy Shoes
    Yeezy 350
    Pandora Jewelry
    Pandora
    Yeezy
    Yeezy Shoes
    Yeezy Sneakers
    Yeezy
    Yeezy Shoes
    Yeezy 350
    Pandora Jewelry
    Pandora
    Pandora Jewelry
    Yeezy
    Ray Bans
    Ray Bans Sunglasses
    Ray Ban Outlet
    Pandora Jewelry
    Yeezy Slides
    Adidas Yeezy
    Yeezy Shoes
    Yeezy 350
    Yeezy
    Yeezy 350
    V12

  4. I’m no longer sure where you are getting your info, but good topic.
    I needs to spend some time finding out much more or understanding more.
    Thanks for wonderful information I was on the lookout for this info for my mission.

  5. I have been surfing online more than 2 hours today, yet I never found any interesting article like
    yours. It is pretty worth enough for me. Personally, if all website owners and bloggers made
    good content as you did, the internet will be much more
    useful than ever before.

  6. I’d have to examine with you here. Which is not one thing I usually do! I take pleasure in reading a post that may make folks think. Additionally, thanks for permitting me to comment!

  7. Pingback: blackhatlinks.com