The default Magento 2 does not show FromDate or ToDate on the detail page from the assigned catalog price rule. If you want to show these dates on the detail page then follow the below steps.
Step 1: Create a registration file like Magemonkeys/Catalogrule/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_Catalogrule', __DIR__ ); |
Step 2: Create module file like Magemonkeys/Catalogrule/etc/module.xml
1 2 3 4 |
<?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_Catalogrule" setup_version="1.0.0"/> </config> |
Step 3: Create block file like Magemonkeys/Catalogrule/Block/Rule.php
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 |
<?php namespace Magemonkeys\Catalogrule\Block; use Magento\Framework\View\Element\Template; use Magento\Framework\Registry; use Magento\Catalog\Model\Product; use Magento\Framework\Exception\LocalizedException; class Rule extends \Magento\Framework\View\Element\Template { protected $_registry; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory $ruleFactory, \Magento\Framework\Registry $registry, array $data = [] ) { $this->_registry = $registry; $this->_ruleFactory = $ruleFactory; parent::__construct($context, $data); } public function getCatalogRuleId() { //die('aaaaa'); $catalogRuleCollection = $this->_ruleFactory->create() ->addFieldToFilter('is_active',1); return $catalogRuleCollection; $resultProductIds = []; foreach ($catalogRuleCollection as $catalogRule) { $productIdsAccToRule = $catalogRule->getMatchingProductIds(); //echo json_encode($productIdsAccToRule); exit; $websiteId = $this->_storeManager->getStore()->getWebsiteId(); foreach ($productIdsAccToRule as $productId => $ruleProductArray) { if (!empty($ruleProductArray[$websiteId])) { $resultProductIds[$productId] = $catalogRule->getData(); } } return $resultProductIds; } } public function getCurrentProduct() { return $this->_registry->registry('current_product'); } public function getCatalogPriceRuleProductIds() { $storeManager = \Magento\Framework\App\ObjectManager::getInstance()->create( '\Magento\Store\Model\StoreManagerInterface' ); $catalogRule = \Magento\Framework\App\ObjectManager::getInstance()->create( '\Magento\CatalogRule\Model\RuleFactory' ); $websiteId = $storeManager->getStore()->getWebsiteId();//current Website Id $resultProductIds = []; $catalogRuleCollection = $catalogRule->create()->getCollection(); $catalogRuleCollection->addIsActiveFilter(1);//filter for active rules only foreach ($catalogRuleCollection as $catalogRule) { $productIdsAccToRule = $catalogRule->getMatchingProductIds(); foreach ($productIdsAccToRule as $productId => $ruleProductArray) { if (!empty($ruleProductArray[$websiteId])) { $resultProductIds[$productId]['rulename'] = $catalogRule->getName(); $resultProductIds[$productId]['fromdate'] = $catalogRule->getFromDate(); $resultProductIds[$productId]['todate'] = $catalogRule->getToDate(); } } } return $resultProductIds; } } |
Step 4: Create layout file like Magemonkeys/Catalogrule/view/frontend/layout/catalog_product_view.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0"?> <!-- /** * Copyright © 2018 Porto. All rights reserved. * See COPYING.txt for license details. */ --> <page layout="2columns-right" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="product.info.main"> <block class="Magemonkeys\Catalogrule\Block\Rule" name="rule_dates" template="Magemonkeys_Catalogrule::product/view/rule.phtml"/> </referenceContainer> <move element="rule_dates" destination="product.info.main" before="product.info.price"/> </body> </page> |
Step 5 : Create template file like Magemonkeys/Catalogrule/view/frontend/templates/product/view/rule.phtml
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 |
<?php $result=$block->getCatalogRuleId(); $product=$block->getCurrentProduct(); $collection=$block->getCatalogPriceRuleProductIds(); $fromdate = ''; $todate = ''; $flag = false; foreach ($collection as $key => $collectionnew) { if ($product->getId() == $key) { $fromdate = $collectionnew['fromdate']; $todate = $collectionnew['todate']; $flag = true; break; } } ?> <?php if($flag){ ?> <div class="promotions-date"> <h3 class="promotions"><?php echo __("Promotion"); ?></h3> <span class="fromdate"><?php echo __("Start Date : "); ?><?php echo $fromdate; ?></span> <span class="todate"><?php echo __("End Date : "); ?><?php echo $todate; ?></span> </div> <?php } ?> <style type="text/css"> .promotions-date { margin-bottom: 20px; } .promotions-date .promotions { margin-top: 0; color: #333; } .promotions-date span { display: block; } </style> |
Step 6: Then run the below commands.
1 2 3 |
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy php bin/magento cache:flush |
That’s it.
Now clean cache and check your product detail page. Now your catalog price rule FromDate and ToDate should be visible.
[crayon-63d3e834c58a1349635354/] 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.