Skip to main content
added 2774 characters in body
Source Link
eswar
  • 103
  • 4
  • 16
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.

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.

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.

Source Link
eswar
  • 103
  • 4
  • 16

how to check if product has a shopping cart rule or not in magento 2

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.