Check if given product will trigger any promo?
-
14-04-2021 - |
Question
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\RulesApplier
class
Solution
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!
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange