PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 10, 2022

[FIXED] How do I mark products that match shopping cart price rules

 November 10, 2022     magento, magento-1.7, php, shopping-cart     No comments   

Issue

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..



Answered By - Ahmed
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing