4

I am exploring the possibilities to show active Cart Price rules on a product page. For example, if I have a blue shirt in the category 'shirts' and I create a Cart Price rule for 20% discount on the category 'shirts', is it possible to check this for a product?

I'm trying to fetch all active cart price rules for a product. In the example of the shirt, I want to show 'Get 20% discount on all shirts' on that specific product page.

I explored the sales-rule module but did not find a possible solution. It should be possible right?

2
  • If this is the your requirement then create catalog price rule, why are you creating cart price rule ??? Commented Dec 19, 2017 at 11:22
  • @MurtuzaZabuawala if I create a catalog price rule, the new price is directly shown on catalog pages. I want to show it as a discount in the cart.
    – Silvan
    Commented Dec 19, 2017 at 11:23

3 Answers 3

5

I got this fixed by using: \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $collection.

I retrieve all rules with:

$rules = $this->collection->create();

You could also filter this collection on Website and store filter.

After you retrieve the collection you can loop through all rules and check if you could apply the rule for the current product:

$product->setProductId($product->getId());

    /** @var \Magento\SalesRule\Model\Rule $rule */
    foreach ($rules as $rule) {
        try {
            if ($rule->getActions()->validate($product)) {
                $activeRules[] = $rule;
            }
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            continue;
        }
    }
4

Instead of Rule module you should look in

// Magento\Quote\Model\Quote\Item    
$item->getAppliedRuleIds() 

Cart contains items, items hold data about applied rules. From that point you will be able to say what rules are applied to a given item and you can do whatever you wish with that information.

2
  • Thanks for the answer. I see we can get this from a Quote item, but how can I get this for a normal product?
    – Silvan
    Commented Dec 15, 2017 at 10:49
  • You can't do that directly. All conditions for cart rules are serialized in conditions_serialized (salesrule table). You can try to deserialize these data and deduce what products if any and under what circumstances will trigger cart discount but that is awkward. Maybe a better solution would be to store that data directly on product. Commented Dec 15, 2017 at 11:29
0

Try This Code

protected $quoteRepository;
protected $ruleData;

public function __construct(
    ...................................................................
    \VendoreName\ModuleName\Model\CustomRuleDataFactory $quoteRepository,
    \Magento\Quote\Api\CartRepositoryInterface $ruleData,
    ...................................................................
) {
    ...................................................................
    $this->quoteRepository = $quoteRepository;
    $this->ruleData = $ruleData;
    ...................................................................
}

public function execute()
{
    $address = 0;
    $quotesVal = 15
    $quotesData = $this->quoteRepository->get($quotesVal);
    $customerId = (int) $quotesData->getCustomerId();
    $productCount = 0;
    foreach ($quotesData->getAllItems() as $quotesDataKey => $quotesDataVal) {
        $address = $quotesDataVal->getAddress();
        $productCount++;
        break;
    }
    if ($productCount) {
        $address->setCachedItemsAll($quotesData->getAllItems());
        $cartRuleData = $this->ruleData->create()->getCollection();
        foreach ($cartRuleData as $ruleKey => $ruleVal) {
            if ($ruleVal->getConditions()->validate($address)){ // if ($ruleVal->getActions()->validate($address))
                echo "Rule Applied";
            }
        }
    }
}

I Hope This Helps You.

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.