afro-Nija
afro-Nija

Reputation: 195

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

Upvotes: 1

Views: 12631

Answers (2)

Pallavi
Pallavi

Reputation: 347

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!

Upvotes: 2

afro-Nija
afro-Nija

Reputation: 195

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

Upvotes: 6

Related Questions