Check if product is new in Magento based on the calender Date

We must need to create a helper for executing this functionality

<?php

namespace Vendor\Module\Helper;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product as ModelProduct;
use Magento\Store\Model\Store;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

class HelperName extends \Magento\Framework\Url\Helper\Data
{

    /**
     * @var TimezoneInterface
     */
    protected $localeDate;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        TimezoneInterface $localeDate
    ) {
        $this->localeDate = $localeDate;
        parent::__construct($context);
    }

    public function isProductNew(ModelProduct $product)
    {
        $newsFromDate = $product->getNewsFromDate();
        $newsToDate = $product->getNewsToDate();
        if (!$newsFromDate && !$newsToDate) {
            return false;
        }

        return $this->localeDate->isScopeDateInInterval(
            $product->getStore(),
            $newsFromDate,
            $newsToDate
        );
    }
}

And in the list phtml file we have to to add the helper

and for the condtion we need check the condition

$helper = $this->helper('Vendor\Module\Helper\HelperName');



<?php if($helper->isProductNew($_product)): ?>
    Your label code here
<?php endif; ?>

Leave a comment

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