4

I have created 3 catalog rules for category.

  1. Shoes Category: 5% Off , Catalog Rule Name: Shoes Offer
  2. Toys Category: 10% Off, Catalog Rule Name: Toys Offer
  3. Fashion Accessories: 8% Off, Catalog Rule Name: Fashion Acc. Offer

So all the product of that categories are showing discounted price in category. On cart page i want to show which catalog rule is applied to item.

Is there any way to check on cart item that which catalog rule is applied to it?

How can i check which catalog rule is applied to item and how much discount is applied?

Any help would be appreciated.

1 Answer 1

0

I found answer on StackOverflow https://stackoverflow.com/questions/51304062/how-to-get-the-catalog-price-rule-programmatically-in-magento2

 /**
 * @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;
}

Your Answer

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

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