If you need to display a new label of the product on the list page, then you can do it via helper file.
Here I have made one extension for that.
1, First you need to create module directories on app/code.
2. Then add registration.php in app/code/vender/module/.
<?php MagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'vender_module', __DIR__ );
3. The next step is to add module.xml file in app/code/vender/module/etc/
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="vender_module" setup_version="1.0.0"> </module> </config>
4. In the final step, you need to create one helper file Newlabel.php in app/code/vender/module/Helper/
<?php
namespace vendermoduleHelper;
use MagentoFrameworkStdlibDateTimeTimezoneInterface;
class Newlabel extends MagentoFrameworkUrlHelperData
{
/**
* @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
);
}
}
After creating all the above files, run setup:upgrade command
Then, you will able to add a new label on your override list.phtml file in product loop) as per below.
<?php $helper = $this->helper('vendormoduleHelperNewlabel');
if($helper->isProductNew($_product)): ?>
<div class="lable newlbl">
<?php echo __('New'); ?>
</div>
<?php endif; ?>
Hope my post will helped you to enrich your Magento knowledge.

