Add module.xml file in app/code/Magemonkeys/ProductLabels/etc and copy the following code in it:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magemonkeys_ProductLabels" setup_version="1.0.1">
</module>
</config>
Add registration.php in app/code/Magemonkeys/ProductLabels and copy the following code in it:
<?php MagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'Magemonkeys_ProductLabels', __DIR__ );
Now add Labels.php file in app/code/Magemonkeys/ProductLabels/Helper and copy the following code in it:
<?php
namespace MagemonkeysProductLabelsHelper;
use MagentoCatalogModelProduct as ModelProduct;
use MagentoStoreModelScopeInterface;
use Zend_Date;
class Labels extends MagentoFrameworkUrlHelperData
{
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoFrameworkStdlibDateTimeDateTime $date
) {
$this->date = $date;
parent::__construct($context);
}
public function isProductNew(ModelProduct $product)
{
$from = new Zend_Date($product->getNewsFromDate());
$to = new Zend_Date($product->getNewsToDate());
$now = new Zend_Date($this->date->gmtTimestamp());
if(!empty($product->getNewsFromDate()) && empty($product->getNewsToDate()))
{
return $from->isEarlier($now);
}
else
{
return ($from->isEarlier($now) && $to->isLater($now));
}
return (boolean) false;
}
}
Override list.phtml
Now go to vendor/magento/module-catalog/view/frontend/templates/product from the root directory of your store and you will see the list.phtml file. Copy the file and paste it into app/code/Magemonkeys/ProductLabels/view/frontend/templates/catalog/product.
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use MagentoFrameworkAppActionAction;
// @codingStandardsIgnoreFile
?>
<?php
/**
* Product list template
*
* @var $block MagentoCatalogBlockProductListProduct
*/
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('MagentoCatalogHelperOutput');
?>
<?php if (!$_productCollection->count()): ?>
<div class="message info empty"><div><?= /* @escapeNotVerified */ __('We can't find products matching the selection.') ?></div></div>
<?php else: ?>
<?= $block->getToolbarHtml() ?>
<?= $block->getAdditionalHtml() ?>
<?php
if ($block->getMode() == 'grid') {
$viewMode = 'grid';
$imageDisplayArea = 'category_page_grid';
$showDescription = false;
$templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
} else {
$viewMode = 'list';
$imageDisplayArea = 'category_page_list';
$showDescription = true;
$templateType = MagentoCatalogBlockProductReviewRendererInterface::FULL_VIEW;
}
/**
* Position for actions regarding image size changing in vde if needed
*/
$pos = $block->getPositioned();
?>
<div class="products wrapper <?= /* @escapeNotVerified */ $viewMode ?> products-<?= /* @escapeNotVerified */ $viewMode ?>">
<ol class="products list items product-items">
<?php /** @var $_product MagentoCatalogModelProduct */ ?>
<?php foreach ($_productCollection as $_product): ?>
<li class="item product product-item">
<div class="product-item-info" data-container="product-<?= /* @escapeNotVerified */ $viewMode ?>">
<?php
$productImage = $block->getImage($_product, $imageDisplayArea);
if ($pos != null) {
$position = ' style="left:' . $productImage->getWidth() . 'px;'
. 'top:' . $productImage->getHeight() . 'px;"';
}
?>
<?php
$labels = $this->helper('MagemonkeysProductLabelsHelperLabels');
?>
<?php if($labels->isProductNew($_product)){ ?>
<div class="label-new new_label"><p><?php echo __('New'); ?></p></div>
<?php }
<?php // Product Image ?>
<a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1">
<?= $productImage->toHtml() ?>
</a>
<div class="product details product-item-details">
<?php
$_productNameStripped = $block->stripTags($_product->getName(), null, true);
?>
<strong class="product name product-item-name">
<a class="product-item-link"
href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>">
<?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
</a>
</strong>
<?= $block->getReviewsSummaryHtml($_product, $templateType) ?>
<?= /* @escapeNotVerified */ $block->getProductPrice($_product) ?>
<?= $block->getProductDetailsHtml($_product) ?>
<div class="product-item-inner">
<div class="product actions product-item-actions"<?= strpos($pos, $viewMode . '-actions') ? $position : '' ?>>
<div class="actions-primary"<?= strpos($pos, $viewMode . '-primary') ? $position : '' ?>>
<?php if ($_product->isSaleable()): ?>
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form" data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>" action="<?= /* @NoEscape */ $postParams['action'] ?>" method="post">
<input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $postParams['data']['product'] ?>">
<input type="hidden" name="<?= /* @escapeNotVerified */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @escapeNotVerified */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
<?= $block->getBlockHtml('formkey') ?>
<button type="submit"
title="<?= $block->escapeHtml(__('Add to Cart')) ?>"
class="action tocart primary">
<span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
</button>
</form>
<?php else: ?>
<?php if ($_product->isAvailable()): ?>
<div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
<div data-role="add-to-links" class="actions-secondary"<?= strpos($pos, $viewMode . '-secondary') ? $position : '' ?>>
<?php if ($addToBlock = $block->getChildBlock('addto')): ?>
<?= $addToBlock->setProduct($_product)->getChildHtml() ?>
<?php endif; ?>
</div>
</div>
<?php if ($showDescription):?>
<div class="product description product-item-description">
<?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
<a href="<?= /* @escapeNotVerified */ $_product->getProductUrl() ?>" title="<?= /* @escapeNotVerified */ $_productNameStripped ?>"
class="action more"><?= /* @escapeNotVerified */ __('Learn More') ?></a>
</div>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>
</div>
<?= $block->getToolbarHtml() ?>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
{
"[data-role=tocart-form], .form.map.checkout": {
"catalogAddToCart": {
"product_sku": "<?= /* @NoEscape */ $_product->getSku() ?>"
}
}
}
</script>
<?php endif; ?>
<?php endif; ?>
Run CLI Commands
rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/* var/session/* var/view_preprocessed/* pub/static/* php bin/magento setup:upgrade php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:clean php bin/magento cache:flush
Now go to the product list page of your store, you will see the desired result:


