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.
EOedHTRQnksKV
kPilzRtjeq
euLTogBdx
Artificial intelligence creates content for the site, no worse than a copywriter, you can also use it to write articles. 100% uniqueness,7-day free trial of Pro Plan, No credit card required:). Click Here:👉 https://bit.ly/3Py2Iv6
порно чат рулетка. Click Here:👉 http://rt.livepornosexchat.com/
IUtRkGjCMFaObiVy
купоны melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокоды Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет при регистрации на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет без депозита Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод в мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет без депозита Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
купоны мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод при регистрации мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод при регистрации на мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет без депозита Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет на ставку Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод для мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокоды Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
купоны melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мел бет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на бесплатную ставку melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод при регистрации мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет на бесплатную ставку Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод при регистрации на мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокоды Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод для melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мел бет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод mel bet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод при регистрации мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
порно видеочат онлайн. Click Here:👉 http://rt.livepornosexchat.com/
AGlDIJvNOELa
https://over-the-counter-drug.com/# anti nausea medication over the counter
where can i get zithromax over the counter zithromax
https://zithromax.science/# generic zithromax over the counter
amoxil amoxicillin 500 mg without prescription
buy doxycycline doxycycline 50mg or doxycycline without a prescription
http://flcu.mobi/__media__/js/netsoltrademark.php?d=over-the-counter-drug.com doxy
doxycycline 100mg tablets doxycycline hyc 100mg and doxycycline 150 mg doxycycline monohydrate
https://zithromax.science/# zithromax
minocycline online cheap stromectol
https://amoxil.science/# rexall pharmacy amoxicillin 500mg
zithromax 250 price where can i get zithromax over the counter
https://stromectol.science/# stromectol
stromectol for sale ivermectin 3mg dosage
price of doxycycline doxycycline without prescription
https://amoxil.science/# amoxicillin 200 mg tablet
stromectol for sale buy stromectol uk
ivermectin 200mg minocycline 50mg tablets online or stromectol lotion
http://www.lpkinnovation.com/__media__/js/netsoltrademark.php?d=stromectol.science buy ivermectin for humans australia
stromectol sales stromectol canada and purchase ivermectin stromectol pill
buy zithromax online australia buy zithromax online
https://amoxil.science/# amoxicillin capsule 500mg price
generic zithromax purchase zithromax z-pak
https://stromectol.science/# ivermectin pills canada
ivermectin 9 mg minocycline 100 mg online
where can i get amoxicillin cheap amoxil
https://doxycycline.science/# price of doxycycline
zithromax tablets for sale zithromax capsules
https://zithromax.science/# zithromax tablets for sale
doxy 200 doxy 200 or generic doxycycline
http://loanco.net/__media__/js/netsoltrademark.php?d=doxycycline.science buy doxycycline 100mg
doxycycline 50 mg generic doxycycline and doxycycline generic doxycycline hyclate 100 mg cap
generic doxycycline purchase doxycycline online or doxycycline generic
http://valariefasue.com/__media__/js/netsoltrademark.php?d=doxycycline.science buy doxycycline online uk
generic doxycycline buy cheap doxycycline and doxycycline hyclate 100 mg cap generic for doxycycline
875 mg amoxicillin cost amoxicillin 500mg buy online uk or where can i buy amoxicillin online
http://kataowang.com/__media__/js/netsoltrademark.php?d=amoxil.science amoxicillin 500mg capsules uk
amoxicillin in india buy amoxicillin 500mg online and amoxicillin 50 mg tablets amoxicillin 250 mg capsule
order doxycycline online doxycycline
https://doxycycline.science/# how to buy doxycycline online
stromectol for sale ivermectin 2ml
order zithromax without prescription buy zithromax canada or can i buy zithromax over the counter
http://www.loginbooking.com/__media__/js/netsoltrademark.php?d=zithromax.science zithromax capsules australia
buy zithromax 500mg online average cost of generic zithromax and buy generic zithromax no prescription zithromax canadian pharmacy
https://amoxil.science/# amoxicillin generic brand
stromectol what is minocycline 50 mg used for
generic stromectol stromectol pills
https://doxycycline.science/# online doxycycline
zithromax for sale zithromax online paypal
cost of ivermectin 1% cream stromectol for sale or buy ivermectin pills
http://resharepoint.com/__media__/js/netsoltrademark.php?d=stromectol.science buy minocycline 100 mg online
ivermectin ireland stromectol nz and ivermectin 4 mg ivermectin price uk
https://amoxil.science/# amoxicillin 500 capsule
stromectol how much it cost stromectol pills
https://zithromax.science/# zithromax pill
order amoxil buy amoxicillin 500mg canada
Read information now. Get here.
ivermectin where to buy
Best and news about drug. Actual trends of drug.
Read information now. Top 100 Searched Drugs.
https://stromectolst.com/# stromectol tablets for humans
Get information now. All trends of medicament.
Medscape Drugs & Diseases. Long-Term Effects.
ivermectin cream
Best and news about drug. Everything about medicine.
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.
п»їMedicament prescribing information. Everything information about medication.
https://stromectolst.com/# ivermectin buy australia
Best and news about drug. drug information and news for professionals and consumers.
minocycline 50 mg pills online minocycline 100mg over the counter or ivermectin tablets uk
http://digestivetherapy.net/__media__/js/netsoltrademark.php?d=stromectol.science generic ivermectin for humans
ivermectin 6 mg tablets ivermectin 0.2mg and ivermectin lice oral ivermectin 12
ivermectin buy ivermectin 5 mg or ivermectin tablets uk
http://signalhealth.net/__media__/js/netsoltrademark.php?d=stromectolst.com stromectol 3mg tablets
ivermectin 200mg ivermectin otc and ivermectin lice oral buy ivermectin cream
Prescription Drug Information, Interactions & Side. Read information now.
https://stromectolst.com/# ivermectin 90 mg
Get here. Generic Name.
stromectol nz stromectol 3 mg price or stromectol tablets for humans
http://floridamiddleschool.net/__media__/js/netsoltrademark.php?d=stromectolst.com stromectol otc
ivermectin new zealand where to buy stromectol online and stromectol online pharmacy ivermectin oral
Get information now. Learn about the side effects, dosages, and interactions.
ivermectin 1% cream generic
What side effects can this medication cause? Drugs information sheet.
Medicament prescribing information. Some are medicines that help people when doctors prescribe.
stromectol 3 mg dosage
Medicament prescribing information. Everything what you want to know about pills.
minocycline 50mg without a doctor buy minocycline 100mg tablets or buy stromectol pills
http://mailpcr.com/__media__/js/netsoltrademark.php?d=stromectol.science minocycline 50 mg para que sirve
ivermectin tablet price ivermectin pills canada and price of ivermectin liquid does minocycline cause weight gain
Cautions. Top 100 Searched Drugs.
ivermectin cream
earch our drug database. Drugs information sheet.
Commonly Used Drugs Charts. earch our drug database.
ivermectin uk coronavirus
Cautions. Read now.
Everything what you want to know about pills. Definitive journal of drugs and therapeutics.
ivermectin 1 cream
Get here. Some trends of drugs.
Some are medicines that help people when doctors prescribe. Drug information.
https://stromectolst.com/# generic ivermectin cream
Read information now. What side effects can this medication cause?
ivermectin 9 mg tablet ivermectin 3mg or stromectol prices
http://bruckheimer.info/__media__/js/netsoltrademark.php?d=stromectol.science ivermectin 3mg dosage
order minocycline 50 mg minocycline 100 mg pills and ivermectin brand stromectol ivermectin buy
Get here. safe and effective drugs are available.
ivermectin 3 mg tabs
earch our drug database. Definitive journal of drugs and therapeutics.
Read information now. drug information and news for professionals and consumers.
ivermectin usa price
Drug information. Actual trends of drug.
ivermectin 3 mg tablet dosage ivermectin lice oral or stromectol 3mg cost
http://lakesapartments.com/__media__/js/netsoltrademark.php?d=stromectolst.com buy ivermectin pills
stromectol tablets for humans can you buy stromectol over the counter and ivermectin over the counter canada price of ivermectin
Read information now. What side effects can this medication cause?
stromectol ireland
Read information now. Get information now.
Get information now. Comprehensive side effect and adverse reaction information.
https://stromectolst.com/# stromectol prices
Drug information. Get warning information here.