0

Is there any possibility in Magento 2 to check if given product will trigger any promotion? I've tried this way. Products in given cart are qualifying to promotion but I got empty array:

  foreach ($products as $product) {
        $rulesApplied[] = $this->rulesApplier->applyRules($product, $rulesCollection, false, "");
    }

RulesApplier is Magento\SalesRule\Model\RulesApplierclass

1

Please try the below code if it helps! It will check if the product qualifies for any rules or not and return all the applicable rules to the product in the array.

<?php

    /**
     * @var \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory
    */
    protected $rulesCollection;

    public function __construct(
        ...
        \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $rulesCollection,
        ...
    ) {
        ...
        $this->rulesCollection = $rulesCollection;
        ...
    }

    public function getActiveRulesProduct($product)
    {
        $getRules = $this->rulesCollection->create();

        foreach ($getRules as $rule) {
            
            if ($rule->getActions()->validate($product)) {
                $arrRules[] = $rule;
            }           
        }
    }

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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