1

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

Adeel
  • 2,901
  • 7
  • 24
  • 34
afro-Nija
  • 185
  • 1
  • 4
  • 17

2 Answers2

5

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

afro-Nija
  • 185
  • 1
  • 4
  • 17
2

For getting all rules applied in your cart:

Class <your classname>
{
protected $_item;

public function __construct(
    ...
    \Magento\Quote\Model\Quote\Item $item
    ...
) {
    ...
    $this->_item = $item;
    ...
}

public function GetAppliedRulesDetails() {
         $appliedIds = $this->_item->getAppliedRuleIds();
         /* here you need to load the results ids and get required details */
         }

}

You can check vendor/magento/module-sales-rule/Observer/SalesOrderAfterPlaceObserver.php file for looping through rules.

What I see in your code is , you are trying to call $resource->getRulesFromProduct() and your class is \Magento\CatalogRule\Model\Rule. Try calling \Magento\CatalogRule\Model\ResourceModel\Rule instead. This should work!

Pallavi
  • 347
  • 1
  • 4
  • 12
  • Thanks Pallavi, but I am more interested in catalog price rules - not sales price rule. – afro-Nija Jul 12 '18 at 14:57
  • 1
    @afro-Nija, Okey, aplogize for misunderstaning I thought you need all applied Rule ids in cart. However what I see in your code is , you are trying to call $resource->getRulesFromProduct() and your class is \Magento\CatalogRule\Model\Rule. Should it not be \Magento\CatalogRule\Model\ResourceModel\Rule? – Pallavi Jul 12 '18 at 15:10
  • Wow Pallavi, you nailed it i will like to mark this suggestion as the answer, could you put it as the answer to mark it. – afro-Nija Jul 12 '18 at 15:47
  • @afro-Nija, updated my answer! Glad that it helped :) – Pallavi Jul 13 '18 at 11:51