Search code examples
phpmagentomagento-1.7shopping-cart

How do I mark products that match shopping cart price rules


I am using shopping cart price rules in my custom module, every thing is working fine I just want to get product ids 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 product. How can I mark the products ids against every rule?


Solution

  • 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) {
            // 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

    $validator = Mage::getModel('modulename/validator')
                   ->init($customer->getWebsiteId(), $customerGroupId);
    
    $v                   = $validator->process($quote);
    $_appliedProductsIds = $v->_appliedProductsIds;
    

    Hop this will help someone..