2
votes

I had to replicate coupon functionality in which the user can able to apply the coupon code and see the discount price in product page itself before adding that product to cart.

So i used ajax call to load the coupon code , its rule conditions by like the following way

<?php
-----

    $oCoupon = Mage::getModel('salesrule/coupon')->load($couponcode, 'code');
    if($oCoupon){
            $oRule     = Mage::getModel('salesrule/rule')->load($oCoupon->getRuleId());
            $rule_arr       = unserialize($oRule->getConditionsSerialized());
            $actions_serialized = unserialize($oRule->getData("actions_serialized"));
            $conditions = $rule_arr['conditions'];
            foreach($conditions as $condition){
                $conditions1 = $condition['conditions'];
                $type        = $condition['type'];
                foreach($conditions1 as $condition1){
                    $type     = $condition1['type'];
                    $value    = $condition1['value'];

                }
            }
        }
    }

----

Here i validated every possible rule - but still its not validating all defined rules and its not stable.

Is there anyway I can able to validate a product and coupon rules - or is there any extension which does validation against one product and coupon rules. Please help me

2

2 Answers

2
votes

Since you can apply a code to a cart/quote like this, I would suggest looking in these models for how it actually validates a code on a product. (working backwards)

Mage::getSingleton('checkout/cart')
    ->getQuote()
    ->setCouponCode($couponcode)
    ->collectTotals()
    ->save();
1
votes

I suggest to look at this free extension for inspiration : http://www.magentocommerce.com/magento-connect/estimate-shipping-on-the-product-page.html

It allow showing shipping estimation in product page by simulating an empty cart, adding the product and collecting shipping rate.

What you have to do is simulate the empty cart, add the product, apply the coupon (with Jared Kipe's method) and get the total.

This module will inspire you and give you method to emulate the empty cart.