Let’s initiate a discussion!!
Add module.xml file in app/code/Magemonkeys/ProductLabels/etc and copy the following code in it:
1 2 3 4 5 |
<?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:
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_ProductLabels', __DIR__ ); |
Now add Labels.php file in app/code/Magemonkeys/ProductLabels/Helper and copy the following code in it:
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php namespace Magemonkeys\ProductLabels\Helper; use Magento\Catalog\Model\Product as ModelProduct; use Magento\Store\Model\ScopeInterface; use Zend_Date; class Labels extends \Magento\Framework\Url\Helper\Data { public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $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; } } |
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.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ use Magento\Framework\App\Action\Action; // @codingStandardsIgnoreFile ?> <?php /** * Product list template * * @var $block \Magento\Catalog\Block\Product\ListProduct */ ?> <?php $_productCollection = $block->getLoadedProductCollection(); $_helper = $this->helper('Magento\Catalog\Helper\Output'); ?> <?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 = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW; } else { $viewMode = 'list'; $imageDisplayArea = 'category_page_list'; $showDescription = true; $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::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 \Magento\Catalog\Model\Product */ ?> <?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('Magemonkeys\ProductLabels\Helper\Labels'); ?> <?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; ?> |
1 2 3 4 5 6 |
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:
[crayon-641fa744bd90d932064647/] 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.