문제

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

도움이 되었습니까?

해결책

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top