1

I would like to get the current catalog rule for the product on the product page. I want to be able to output the name on the page.

I know this is possible in magento 1 but cannot find any function in code for magento 2.

0

This table catalogrule_product holds all the catalog rules applied to a particular product with the discount amount.

You could use this resource model Magento\CatalogRule\Model\ResourceModel\Rule::getRulesFromProduct($date, $websiteId, $customerGroupId, $productId) to get rules applied to a product.

<?php
namespace Vendor\Module\Model;

class Sample extends \Magento\Framework\Model\AbstractModel
{
    ...

    /**
     * @var \Magento\CatalogRule\Model\ResourceModel\Rule
     */
    protected $ruleResource;

    ...

    public function __construct(
        ...
        \Magento\CatalogRule\Model\ResourceModel\Rule $rule
    ) {
        ...
        $this->ruleResource = $rule;
        ...
    }

    ...

    /**
     * @param int|string $date
     * @param int $websiteId
     * @param int $customerGroupId
     * @param int $productId
     */
    public function getRules($date, $websiteId, $customerGroupId, $productId)
    {
        ...
        /** @var [] $rules catalog rules */
        $rules = $this->ruleResource->getRulesFromProduct($date, $websiteId, $customerGroupId, $productId);
        ...
    }

    ...
}
0

class Rule extends \Magento\CatalogRule\Model\Rule {

public function getRuleByProduct($product){
    $priceRules = null;
    $rd = null;
    $productId = $product->getId();
    $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();
    }

    $cacheKey = date('Y-m-d', $dateTs) . "|{$websiteId}|{$customerGroupId}|{$productId}|{$price}";


    $rules =  $this->_getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $product->getId());

    if (!array_key_exists($cacheKey, self::$_priceRulesData)) {
        $rulesData = $this->_getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
        if ($rulesData) {
            foreach ($rulesData as $ruleData) {
                if ($product->getParentId()) { 
                    $rd = $ruleData['rule_id'];
                    $priceRules = $priceRules ? $priceRules : $price;
                    if ($ruleData['action_stop']) {
                        break;
                    }
                } else {
                    $rd = $ruleData['rule_id'];
                    $priceRules = $this->_catalogRuleData->calcPriceRule(
                        $ruleData['action_operator'],
                        $ruleData['action_amount'],
                        $priceRules ? $priceRules : $price
                    );
                    if ($ruleData['action_stop']) {
                        break;
                    }
                }
            }
            return self::$_priceRulesData[$cacheKey] = $rd;
        } else {
            self::$_priceRulesData[$cacheKey] = null;
        }
    } else {
        return self::$_priceRulesData[$cacheKey];
    }
    return null;

    return $rules;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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