0
I have tried these

$currentDate = $this->_stdTimezone->date()->format('Y-m-d');
        $rules = $this->collection->create()
                        ->addFieldToFilter('is_active', 1)
                        ->addFieldToFilter('from_date', ['lteq' => $currentDate])
                        ->addFieldToFilter('from_date', ['neq' => 'NULL']);
        $product = $this->_coreRegistry->registry('product');
            foreach ($rules as $rule) {
            try {
                if ($rule->getActions()->validate($product)) {
                        $activeRules[] = $rule;

                }
            } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
                continue;
            }
        }
        $rulesApplied = "";
        foreach ($rules as $rule) {
                $code = $rule->getCode();
                $rulesApplied .= "<strong>".$code."</strong>"."</br>";
        }
        return $rulesApplied;

but i am getting all the rules which are satisfying the given conditions.I need only the rules which are applied to a products.

2 Answers 2

1

Based on product sku i am able to display the promo code rule

class Coupons extends \Magento\Framework\View\Element\Template
{
/**
 * Core registry
 *
 * @var \Magento\Framework\Registry
 */
protected $_coreRegistry = null;

/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory  $collection,
    \Magento\Framework\Stdlib\DateTime\Timezone $_stdTimezone,
    array $data = []
) {
    $this->_coreRegistry = $registry;
    $this->collection = $collection;
    $this->_stdTimezone = $_stdTimezone;
    parent::__construct($context, $data);
}


public function getDiscount()
{

    $currentDate = $this->_stdTimezone->date()->format('Y-m-d');
    $rules = $this->collection->create()
                    ->addFieldToFilter('is_active', 1)

                    ->addFieldToFilter('from_date', ['lteq' => $currentDate])
                    ->addFieldToFilter('from_date', ['neq' => 'NULL']);

    $product = $this->_coreRegistry->registry('product');
     /** @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;
        }
    }
    $rulesApplied = "";
    $productsku = $product->getSku();
    foreach ($rules as $rule) {
        if ($rule->getIsActive()) {
        $condition_ser = $rule->getConditionsSerialized();
        $unserialized_conditions = json_decode($condition_ser);
        if(isset($unserialized_conditions->conditions[0]->conditions[0]->value)){
        $explode_array = explode(",", $unserialized_conditions->conditions[0]->conditions[0]->value); 
        $code="";
        for($j=0;$j<count($explode_array);$j++)
            {

                if(trim($explode_array[$j])==$productsku)
                {

                    $code.= $rule->getCode();
                    $rulesApplied .= "<strong>".$code."</strong>"."</br>";
                }

            }
        }

        }               
    }

    return $rulesApplied;
}
} 
0

There could be different conditions which identify a assigned or affected product.

I would implement such a requirement by defining the product attribute which has to be used/set in a action condition within a cart rule and than validate those condition by looping all currently active rules.

Best practice: SKU

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.