2

I am using shopping cart price rules in my custom module, everything is working fine. I just want to get product ID's against every rule.

If there are 10 products in cart and on 3 of them had some rules applied, rule #1 on 2 products and rule #2 on 1 products.

How can I mark the products ID's against every rule?

3
  • did you set rule priority and "stop further rule processing" to "no" ? Jan 23, 2014 at 8:29
  • Can you clarify your question? If I understand you correctly, you want to add the product ID to the rule, if that product validated on that rule? As far as I am concerned, that is the wrong way round. You need to set the rule in the product using applied_rule_ids, thus the product will tell you what rule it validate against successfully.
    – ProxiBlue
    Jan 23, 2014 at 8:44
  • @ProxiBlue Yes you understood me correctly but how can I set the rule id in product or save it in some other table ?
    – Ahmed
    Jan 23, 2014 at 9:25

2 Answers 2

2

I solved it like that

Just change process function in Validator.php to

public function process($_quote) {
    $i               = 0;
    $quote           = $_quote;
    $customerSession = Mage::getSingleton('customer/session');
    foreach ($this->_rules as $rule) {
        // @var $rule FME_Affiliates_Model_Program
        // already tried to validate and failed
        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
        }

        $this->_appliedProductsIds[]                 =  Mage::getSingleton('checkout/session')->getReturnProductRuleValues();
        $this->_appliedProductsIds[$i]['program_id'] =  $rule->getProgramId();
        Mage::getSingleton('checkout/session')->unsReturnProductRuleValues($this->_ReturnValues);
        $i = $i + 1;
    }
    return $this;
}

And validate function in Found.php to

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;
    }

$this->_ReturnValues['Product_Id'] = $this->_ProductId;
if(!empty($this->_ProductId)) {
    $this->_ReturnValues['Bool'] = true;
} else {
    $this->_ReturnValues['Bool'] = false;
}

    if ($found && $true) {
        // found an item and we're looking for existing one
    Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
        return true;
    } elseif (!$found && !$true) {
        // not found and we're making sure it doesn't exist
    Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
        return true;
    }
Mage::getSingleton('checkout/session')->setReturnProductRuleValues($this->_ReturnValues);
    return false;
}

And access the values in Observer with Vadidator class object like $appliedProductsIds = $validator->_appliedProductsIds;

Hop this will help someone..

1

Firstly, you need to do this the other way round. Rather than placing to the rule, the ids of the products that validated, you need to place the id of the rule that validated against the product. This then allows you to determine what rules applied on a product.

In core magento shopping cart rules, the id(s) of any rules are simply placed in a product attribute, called applied_rule_ids, as a comma separated list. This thus allows for a product to have many rules applied to it.

So, that should already have answered your question, use $product->setAppliedRuleIds() to save the id(s) of your applied rules.

However, if in your custom module, you have created a separate rule based system, you would need to add your own attribute to the products, or else your rule id will clash with any cart rule ids.

Having the rule ids stored against the products allows you to later instantiate the rule object again, and potentially do some further actions on the product with the rule logic.

Hope that all helps.

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.