5

I want need to show New label on new product in product list page.

Help me for the achieve this task.

How can it will Show in magento2 ?

3
  • You mean if we set product new fields in admin, this label will show? Commented May 3, 2016 at 14:50
  • I need to show New as label in product list page, product set as new from admin by product set as new from to date. Commented May 3, 2016 at 17:01
  • Is it working with my answer? :) Commented May 4, 2016 at 13:14

2 Answers 2

12

Propably the best option is to add new helper with following code:

<?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
        );
    }
}

Then in .phtml file use this:

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

and in products foreach:

<?php if($helper->isProductNew($_product)): ?>
    Your label code here
<?php endif; ?>
3
  • 1
    Any reason to use \Magento\Framework\Url\Helper\Data instead of \Magento\Framework\App\Helper\AbstractHelper? Commented Feb 7, 2018 at 16:21
  • hi @ideo how do we show 'hot' and 'sale' labels for the product in the category list page do u have any idea? Commented May 7, 2018 at 5:43
  • What about the full page cache? As the product list might be cached, you have to ensure it is purged when the product is now longer new.
    – Alex
    Commented Jan 22, 2019 at 13:35
9

Create helper and add code.

<?php

namespace Vendor\Module\Helper;

use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

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

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

    public function __construct(
        TimezoneInterface $localeDate
    ) {
        $this->localeDate = $localeDate;
    }

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

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

Add below code to list.phtml file (in foreach)

<?php $helper = $this->helper('Vendor\Module\Helper\Newlabel'); ?>
<?php if($helper->isProductNew($_product)): ?>
<div class="new-lable">
    <?php echo __('New'); ?>
</div>
<?php endif; ?>

And finally it will show NEW label in product list page. Working Great.

3
  • hi @suresh how do we show hot and sale labels ? Commented May 7, 2018 at 5:36
  • What about the full page cache? As the product list might be cached, you have to ensure it is purged when the product is now longer new.
    – Alex
    Commented Jan 22, 2019 at 13:35
  • Thanks for nice code!!! Commented Sep 1, 2020 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.