Magento 2 You can retrieve the active catalog rules of the system. Using Active rules, You can identify a number of rules that are active and working for the catalog category pages.
To identify, an active rule you need to filter the catalog collection by is_active field. Value 1 contains the active and 0 used to disable the rule in the system.
is_active field used to track enable or disable catalog rule in the database catalogrule table.
<?php namespace Jesadiya\CatalogActiveRule\Model; use Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory; class EnableCatalogRule { /** * @var RuleCollectionFactory */ protected $ruleCollectionFactory; public function __construct( RuleCollectionFactory $ruleCollectionFactory ) { $this->ruleCollectionFactory = $ruleCollectionFactory; } public function getAllActiveCatalogRule() { $catalogActiveRule = $this->ruleCollectionFactory->create()->addFieldToFilter('is_active', 1); return $catalogActiveRule; } }
Calling from the template file or any PHP class,
$catalogRule = $this->getAllActiveCatalogRule(); foreach($catalogRule as $rule) { var_dump($rule->getData()); }
Iterate over a for loop for the resultant catalog rule object to fetch individual active rule by detail information.