2

I have the sales rules collection in a helper file got the rules collection from this bit of code. Now I want to validate if the product id needs to validate against sales rule.

Constructor contains this

\Magento\SalesRule\Model\RuleFactory $ruleFactory,
$this->ruleFactory = $ruleFactory;

and the public function contains this

$_rules = $this->ruleFactory->create()->getCollection();
foreach($_rules as $rule){
            $fromDate = $rule->getFromDate();
            $toDate = $rule->getToDate();
            if (isset($fromDate) && $_currentTime >= strtotime($fromDate)) {
                if (isset($toDate)) {
                    if (strtotime($toDate) >= $_currentTime) {
                        print_r($products_id);
                        print_r($rule->getData());
                    }
                }
            }
        }

Can any one help on this.

2

I found a solution on this below is the way I did and it worked.

$products_id = $id;
    $_rules = $this->ruleFactory->create()->getCollection();

    $_currentTime = strtotime($this->datetime->date());
    $objectManager = $this->_objectManager;

    $html = " ";
    foreach($_rules as $rule){
        $fromDate = $rule->getFromDate();
        $toDate = $rule->getToDate();
        if (isset($fromDate) && $_currentTime >= strtotime($fromDate)) {
            if (isset($toDate)) {
                if (strtotime($toDate) >= $_currentTime) {
                    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($products_id);
                    $item = $objectManager->create('Magento\Catalog\Model\Product');
                    $item->setProduct($product);
                    //Return True if Sales Rule validate product
                    $validate = $rule->getActions()->validate($item);
                    if($validate){
                        if($rule->getId()=='8') $html .= '<span class="coupon-label"><span>'.$rule->getName().'</span></span>';
                    }
                }
            }
        }
    }
  • It will not always work, as it is checking the entire quote Item, Once you set the condition with subselect it will fail :) – Keyur Shah Sep 19 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.