We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

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

<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magemonkeys_Catalogrule',
__DIR__
);

 

Step 2: Create module file like Magemonkeys/Catalogrule/etc/module.xml

<?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

<?php
    namespace MagemonkeysCatalogruleBlock;
    use MagentoFrameworkViewElementTemplate;
    use MagentoFrameworkRegistry;
    use MagentoCatalogModelProduct;
    use MagentoFrameworkExceptionLocalizedException;

    class Rule extends MagentoFrameworkViewElementTemplate
    {
        protected $_registry;

        public function __construct(
            MagentoBackendBlockTemplateContext $context,
            MagentoCatalogRuleModelResourceModelRuleCollectionFactory $ruleFactory,
            MagentoFrameworkRegistry $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 = MagentoFrameworkAppObjectManager::getInstance()->create(
             'MagentoStoreModelStoreManagerInterface'
        );
        $catalogRule = MagentoFrameworkAppObjectManager::getInstance()->create(
             'MagentoCatalogRuleModelRuleFactory'
        );
        
        $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

<?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="MagemonkeysCatalogruleBlockRule" 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

<?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.

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.

field_5bfb909c5ccae

    Recent Articles
    Get a Free Quote

      Full Name

      Email Address

      Mobile Number

      Message

      Let’s initiate a discussion!!

      With Only Agency that provides a 24/7 emergency support.

        Full Name

        Email Address

        Mobile Number

        Message