0

Class Mage_SalesRule_Model_Validator has an object of salesrule/rule_collection, and that object calls a function validate of class Mage_SalesRule_Model_Rule_Condition_Product_Found, what I didn't understand is how the object of salesrule/rule_collection is related to Found class, and if we add any other function to this class and try to access it will throw an Exception Undefined Function, I just want to understand whats going on behind the scenes

UPDATE

Mage_SalesRule_Model_Rule_Condition_Product_Found extends the class Mage_SalesRule_Model_Rule_Condition_Product_Combine but when from process function of the Mage_SalesRule_Model_Validator called it calls the validate function of the Found class Mage_SalesRule_Model_Validator object call to process

try {
   $validator = Mage::getModel('module/validator')
                    ->init($customer->getWebsiteId(), $customerGroupId);
} catch (Exception $e) {
   Mage::log('Exception: ' . $e . ' in ' . __CLASS__ . ' on ' . __LINE__);
}
$v  = $validator->process($quote);

And the process function of Mage_SalesRule_Model_Validator which calls the validate function of Found class

public function process($_quote) 
{
         $quote           = $_quote;
         $customerSession = Mage::getSingleton('customer/session');
         foreach ($this->_rules as $rule) {

            if ($rule->getIsValid() === false) {
              continue;
            }
            if ($rule->getIsValid() !== true) { 
                $rule->afterLoad();
                if (!$rule->validate($quote)) { // quote does not meet rule's conditions , //Call Found.php
                $rule->setIsValid(false);
                continue;
                }
                $rule->setIsValid(true); // passed all validations, remember to be valid
            }
    }
    return $this;
}

And the validate function of Found class

   public function validate(Varien_Object $object) 
   {
//Called form Validator.php
    $all       = $this->getAggregator() === 'all';
    $true      = (bool)$this->getValue();
    $found     = false;

    $Count =  count($object->getAllItems()); 
    $i = 0;
    foreach ($object->getAllItems() as $item) {
        $found = $all ? true : false;
        foreach ($this->getConditions() as $cond) {

            $validated = $cond->validate($item); // Call to Product.php's function 'validate'

            if($validated) {
                $this->_ProductId[] = $item->getProductId();
            }

            if($i == $Count) {
                if ($all && !$validated) {
                $found = false;
                break;
                } elseif (!$all && $validated) {
                $found = true;
                break 2;
                }
            }

                }
            if($i == $Count) {
            if ($found && $true) {
                break;
            }
            }
            $i = $i + 1;
        }
    return false;
}

Now what I didn't understand is if I write any other function in Found class let it be public function Foo() and try to call it from process function of class Validator like

    $rule->Foo();

it will throw an Exception Undefined Function I just want to know the reason why I can't write any function in Found class and call like process Thanks

1

When calling the $rule->validate() you end up calling $rule->getConditions()->validate(). This validates every condition and a condition is an instance of the class Mage_SalesRule_Model_Rule_Condition_Product_Combine or one of it's descendants.
One of the descendants is Mage_SalesRule_Model_Rule_Condition_Product_Found. Depending on the conditions you set for your rule, you may or may not end up in that class.
The module comes in play when you add a condition by selecting Product attribute combination when defining the rule.
See what all models mean in the method Mage_SalesRule_Model_Rule_Condition_Combine::getNewChildSelectOptions

$conditions = array_merge_recursive($conditions, array(
    //this is what you are looking for
    array('value'=>'salesrule/rule_condition_product_found', 'label'=>Mage::helper('salesrule')->__('Product attribute combination')),
    array('value'=>'salesrule/rule_condition_product_subselect', 'label'=>Mage::helper('salesrule')->__('Products subselection')),
    array('value'=>'salesrule/rule_condition_combine', 'label'=>Mage::helper('salesrule')->__('Conditions combination')),
    array('label'=>Mage::helper('salesrule')->__('Cart Attribute'), 'value'=>$attributes),
));
  • Thanks, it helped me a lot to understand how rules work, but as you said Mage_SalesRule_Model_Rule_Condition_Product_Found is decedent of 'Mage_SalesRule_Model_Rule_Condition_Product_Combine' and we can call its validate function as $rule->validate() but I tried to write an other function in that Found class i-e '$rule->MyCustomMethod()' it keep sending me an error Undefined Function. So how can I write custom method and call it through same object $rule – Ahmed Jan 28 '14 at 9:39
  • 1
    $rule is not an instance of Mage_SalesRule_Model_Rule_Condition_Product_Found It is an instance of Mage_SalesRule_Model_Rule. You need to add the method on that class. – Marius Jan 28 '14 at 9:43
  • Sorry but I am bit confused, is 'validate' defined some where to be called by $rule ? because its also a function of Found class but we are calling it through $rule. Or didn't that code mean exactly ? 'array('value'=>'salesrule/rule_condition_product_found', 'label'=>Mage::helper('salesrule')->__('Product attribute combination'))' Thanks for the help – Ahmed Jan 28 '14 at 9:58
  • *Or what that code mean exactly ? – Ahmed Jan 28 '14 at 10:04

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.