1

Simply I want to get the rule_id or catalog rule details that is applied to a product in product details page and product listing page.

I have checked with this function _getRulesFromProduct() from magento-catalog-rule/Model/Rule.php but it return all catalog rule applicable for that product.

I want only one rule which is currently applied to that product.

7
  • Did you pass all the arguments to the getRulesFromProduct function ?
    – aravind
    Jul 17, 2019 at 9:58
  • Yes, $this->_getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $product->getId()); Jul 17, 2019 at 10:09
  • could you share the full code you have implemented for this proces
    – aravind
    Jul 17, 2019 at 10:24
  • public function get($product){ $priceRules = null; $price = $product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue(); $storeId = $product->getStoreId(); $dateTs = $this->_localeDate->scopeTimeStamp($storeId); $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId(); if ($product->hasCustomerGroupId()) { $customerGroupId = $product->getCustomerGroupId(); } else { $customerGroupId = $this->_customerSession->getCustomerGroupId(); } $rules = $this->_getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $product->getId()); return $rules; } Jul 17, 2019 at 10:49
  • This is the code i have written. and return all rules which is applicable to the same product in product details page. Jul 17, 2019 at 12:00

2 Answers 2

0

Try with below way!

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();
        if (in_array("Your currrent productId", $affectedProductIds)) {
            //product has a price rule set for it
        }
    }
    return $resultProductIds;
}

Please don't use objectmanager. it's bad practice! I hope it helps!

8
  • This function will return all product Ids applicable for the product right? i want the rule id Jul 17, 2019 at 11:02
  • Yes! and using condition if (in_array("Your currrent productId", $affectedProductIds)) {} you can match your current product id Jul 17, 2019 at 11:03
  • The function i have written in the comment will return all rule ids which is applicable for the product. i want only single rule which is applied currently. Jul 17, 2019 at 11:06
  • It is not necessary every product have a singe rule there will be multiple rules will be applied on a single product. Jul 17, 2019 at 11:10
  • I have enabled "Discard subsequent rules" to yes in Catalog Price Rule from admin side. So i think only one rule is applied a product at same time right? Jul 17, 2019 at 11:14
0

Try getting rules from the resourceModel instead,

I have used objectmanager try to use it as dependency,

public function get($product){ 
    $priceRules = null; 
    $price = $product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue(); 
    $storeId = $product->getStoreId(); 
    $dateTs = $this->_localeDate->scopeTimeStamp($storeId); 
    $websiteId = $this->_storeManager->getStore($storeId)->getWebsiteId(); 

    if ($product->hasCustomerGroupId()) {
        $customerGroupId = $product->getCustomerGroupId(); 
    } else { 
        $customerGroupId = $this->_customerSession->getCustomerGroupId(); 
    } 

    $resource = $this->_objectManager->create('\Magento\CatalogRule\Model\ResourceModel\Rule');

    $rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId,  $product->getId());

    print_r( $rules); 
} 

Let me know if you have any queries

2
  • In this function it will return array of rules. Not a single one. Jul 17, 2019 at 12:40
  • share the result ..
    – aravind
    Jul 17, 2019 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.