Pregunta

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

¿Fue útil?

Solución

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!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top