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/.
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'vender_module', __DIR__ ); |
3. The next step is to add module.xml file in app/code/vender/module/etc/
1 2 3 4 5 |
<?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/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php namespace vender\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 ); } } |
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.
1 2 3 4 5 6 |
<span class="pun"><?</span><span class="pln">php $helper </span><span class="pun">=</span><span class="pln"> $this</span><span class="pun">-></span><span class="pln">helper</span><span class="pun">(</span><span class="str">'vendor\module\Helper\Newlabel'</span><span class="pun">); </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">$helper</span><span class="pun">-></span><span class="pln">isProductNew</span><span class="pun">(</span><span class="pln">$_product</span><span class="pun">)):</span> <span class="pun">?></span> <span class="tag"><div</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"lable newlbl"</span><span class="tag">></span> <span class="pun"><?</span><span class="pln">php echo __</span><span class="pun">(</span><span class="str">'New'</span><span class="pun">);</span> <span class="pun">?></span> <span class="tag"></div></span> <span class="pun"><?</span><span class="pln">php endif</span><span class="pun">;</span> <span class="pun">?></span> |
Hope my post will helped you to enrich your Magento knowledge.
[crayon-63d3e1dd39eb3426135479/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.