Thursday, January 5, 2012

Apply coupon discount on Original Price not on special price in magento programmatically

Hi,

In Magento Default coupon code will apply to special price if special price is there else it will apply to original price ,
but in some case we need to apply coupon code discount to original price and then apply to special price
EX:- Special price 1000
Original Price :- 2000
Coupon discount is 10%

Then in cart page it will show special price- discountamount( acc to special price) i't 1000-100 = 900

but we need special price- discountamount( acc to Original price) i'e 1000- 200 =800

for that we need to create a event

add this xml code in config.xml in side events tag i'e <events></events>




<!-- event for default coupon but discount will apply for MRP-->
            <salesrule_validator_process>
                <observers>
                    <new_dicount_coupon>
                        <type>singleton</type>
                        <class>Namespcae_Modulename_Model_Observer</class>
                        <method>newcoupondiscountcal</method>
                    </new_dicount_coupon>
                </observers>
            </salesrule_validator_process>



And created Observer.php inside your module/model if not exists else add only this function newcoupondiscountcal($observer) in side Observer class



class Namespcae_Modulename_Model_Observer
{


//event for default coupon but discount will apply for MRP
public function newcoupondiscountcal($observer)
{

$item=$observer['item'];
$rule=$observer['rule'];
$rulePercent = max(0, 100-$rule->getDiscountAmount());
//print_r($rule->getData()); exit;

if($rule->getSimpleAction()=='by_percent') {
$disPer=$rule->getDiscountAmount();
$product=Mage::getModel('catalog/product')->load($item->getProductId());
$ratdisc=($product->getPrice()*$disPer)/100;
$DiscountAmount=$ratdisc+$item->getDiscountAmount();
$BaseDiscountAmount=$ratdisc+$item->setBaseDiscountAmount();

$result = $observer['result'];
$result->setDiscountAmount($DiscountAmount);
$result->setBaseDiscountAmount($BaseDiscountAmount);

}
}


clear cache and apply the code

No comments:

Post a Comment