magento2rulescatalogdiscount

How to get the catalog price rule programmatically in Magento2


Simply I just want to get the catalog price rules applied to products during checkout. I know a lot of solution is out from some sources for Magento 1, an example is this blog https://jutesenthil.wordpress.com/2015/09/28/get-catalog-rule-by-product-id-in-magento/ but trying to get same result in Magento 2 does not seem to be working. my code snippet is below.

/**
 * @param $productId
 * @param $customerGroupId
 * @return mixed
 */
public function getCatalogPriceRuleFromProduct($productId, $customerGroupId)
{
    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    $product = $this->_objectManager->create('\Magento\Catalog\Model\ProductFactory')->create()->load($productId);

    $storeId = $product->getStoreId();

    $store = $this->_store_manager->getStore($storeId);

    $websiteId = $store->getWebsiteId();

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    $date = $this->_objectManager->create('\Magento\Framework\Stdlib\DateTime\DateTime');
    $dateTs = $date->gmtDate();

    /**
     * @var \Magento\CatalogRule\Model\Rule
     */
    $resource = $this->_objectManager->create('\Magento\CatalogRule\Model\Rule');
    // $resource = $this->_objectManager->create('\Magento\CatalogRule\Model\RuleFactory');

    $rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
    /*$rules = $resource->getCollection()
        ->addFieldToFilter('from_time', $dateTs)
        ->addFieldToFilter('to_time', $dateTs)
        ->addFieldToFilter('product_id', $productId)
        ->addFieldToFilter('store_id', $storeId)
        ->addFieldToFilter('website_id', $websiteId)
        ->addFieldToFilter('customer_group_id', $customerGroupId);*/

    return $rules;
}

But always returning null.

Any help or ideas to go about this??


Solution

  • For anyone that requires this solution this is it

    /**
     * @param $productId
     * @param $customerGroupId
     * @return mixed
     */
    public function getCatalogPriceRuleFromProduct($productId, $customerGroupId)
    {
        /**
         * @var \Magento\Catalog\Model\ProductFactory
         */
        $product = $this->_objectManager->create('\Magento\Catalog\Model\ProductFactory')->create()->load($productId);
    
        $storeId = $product->getStoreId();
        $store = $this->_store_manager->getStore($storeId);
        $websiteId = $store->getWebsiteId();
        /**
         * @var \Magento\Framework\Stdlib\DateTime\DateTime
         */
        $date = $this->_objectManager->create('\Magento\Framework\Stdlib\DateTime\DateTime');
        $dateTs = $date->gmtDate();
    
        /**
         * @var \Magento\CatalogRule\Model\ResourceModel\Rule
         */
        $resource = $this->_objectManager->create('\Magento\CatalogRule\Model\ResourceModel\Rule');
    
        $rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
    
        return $rules;
    }
    

    and if you need to get the actual discount amount just use this piece of code as well.

    /**
                         * @var \Magento\CatalogRule\Model\RuleFactory
                         */
                        $rule = $this->_objectManager->create('\Magento\CatalogRule\Model\RuleFactory')->create();
                        $discountAmount = $rule->calcProductPriceRule($product,$product->getPrice());
    

    All thanks to @Pallavi