0

I am trying to find out if the condition that I am going to set in the price rule already exists or not. I tried to check if it is valid or not but couldn't find the correct function I think.

        if($voucherRule->getConditions()->validate($realProduct)){
            echo "<br> Already exists<br>";
            $foundamatch = true;
        }

        if(!$foundamatch){
             $item_found = Mage::getModel('salesrule/rule_condition_product_found')
        ->setType('salesrule/rule_condition_product_found')
        ->setValue(1) // 1 == FOUND
        ->setAggregator('any'); // match ANY conditions
             $skuCondition = Mage::getModel('salesrule/rule_condition_product')
        ->setType('salesrule/rule_condition_product')
        ->setAttribute('sku')
        ->setOperator('==')
        ->setValue($realProduct->getSku());
            $voucherRule->getConditions()->addCondition($item_found);
            $item_found->addCondition($skuCondition);
        }
        $voucherRule->save();

Did not work, it always creates the same conditions over and over again when ever I run the script.

How can I check this?

1 Answer 1

0

I found a way t odo so. See the code below (It works like a charm!):

    try{
        $foundamatch = false;
        $unserializedConditions = unserialize($voucherRule->getData('conditions_serialized'));

        foreach($unserializedConditions['conditions'] as $conditions){
            foreach($conditions['conditions'] as $subcond){
                if($subcond['value'] == $sku)
                {
                    echo "<br>Product SKU condition already Exists!<br>";
                    $foundamatch = true;
                }
            }
        }
        if(!$foundamatch){
            $voucherRule->getConditions()->addCondition($item_found);
            $item_found->addCondition($skuCondition);
        }
        $voucherRule->save();
    }
    catch (Exception $e) {
        die($e->getMessage());
    }
1
  • Feel free to accept your own anser so that the question does not count as unanswered anymore Commented Sep 3, 2015 at 7:34

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.