Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to check which shopping cart rules are applied to product?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.47k
    Comment on it

    In magento if we need to check, if any shopping cart rule is applied to the product or not.
    To do so we can perform it in a test file in our root folder.

    lets perform the same, create a file test.php in root folder and write the below code:


    $product_id = 53;
    $_product= Mage::getModel('catalog/product')->load($product_id);
    $coll = Mage::getResourceModel('salesrule/rule_collection')->load();
    foreach($coll as $rule){
    $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
    $real_quote = Mage::getSingleton('sales/quote')->load($quoteId);        
    $product = Mage::getModel('sales/quote_item')->setQuote($real_quote)->setProduct($_product); 
    foreach($coll as $rule)
      if ($rule->getConditions()->validate($product)) 
       echo $rule->getName().'/'.$rule->getData('discount_amount').'<br>';


    In the above code we have first included the mage.php file which loaded the magento app.
    Then we stored the product id in the parameter $product_id. With the help of the $product_id we loaded the product in the parameter $_product.

    then, in $coll parameter we loaded the sales rules collection. Afterwards we make the entries in the quote tables by setting the quoteid from checkout/session.
    In the $product parameter we set the quote_item table entries by loading the model getModel('sales/quote_item').

    Now, in the foreach loop we perform the validation to check the rules with the product to know which are the rules that are applied on the product.
    In the if loop condition, if the validate returns true then the statement prints the Name and the Discount amount of the rule.

    This way we can use the above code where ever we want in our magento system to perform this operation for some purpose.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: