Magento Forum

   
Page 1 of 2
Applying Custom discount amount in magento
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Hi,

In few case you have to write a code so that custom discount as to apply to cart

either you can ave discount amount in Db or static i am showing the cod for static one

in your custom module etc/config.xml add this code in side global tag

<events
   <
sales_quote_collect_totals_after>
                <
observers>
                    <
set_discount_static>
                        <
type>singleton</type>
                        <class>
Loyalty_Loyalty_Model_Observer</class>
                        <
method>setdiscountamount</method>
                    </
set_discount_static>
                </
observers>
            </
sales_quote_collect_totals_after>  
   
   </
events

and inside model folder create file called Observer.php

class NameSpace_Custommodule_Model_Observer
{
  
public function setdiscountamount($observer)
  
{
  $session 
Mage::getSingleton('checkout/session');
 if(
Mage::helper('customer')->isLoggedIn()) {
  $customer 
Mage::getSingleton('customer/session')->getCustomer();
  
$customer_group=Mage::getModel('customer/group')->load(Mage::getSingleton('customer/session')->getCustomerGroupId())->getCustomerGroupCode();
  
$quote=$observer->getEvent()->getQuote();
  
$isvirtual=0;
    foreach(
$quote->getAllItems() as $item){
     
if($item->getIsVirtual()==1{
     $isvirtual
=1;
     
}
     
if(Mage::getModel('catalog/product')->load($item->getProductId())->getTypeId()=='customproduct'){
     $isvirtual
=1;
     
}
    }
       
   
if($discountAmount>0{
    
   
    
   
   $quote
->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
   ->
setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
   ->
setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
   ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
   ->
save();
   
   
$canAddItems $quote->isVirtual()? ('billing') : ('shipping'); 
   foreach (
$quote->getAllAddresses() as $address{
    
    $address
->setSubtotal(0);
    
$address->setBaseSubtotal(0);
 
    
$address->setGrandTotal(0);
    
$address->setBaseGrandTotal(0);
 
    
$address->collectTotals();
    
    if(
$address->getAddressType()==$canAddItems{
 
     $address
->setSubtotal((float) $quote->getSubtotal());
     
$address->setBaseSubtotal((float) $quote->getBaseSubtotal());
     
$address->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount());
     
$address->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount());
     
$address->setGrandTotal((float) $quote->getGrandTotal());
     
$address->setBaseGrandTotal((float) $quote->getBaseGrandTotal());
     
$address->setDiscountAmount(-$discountAmount);
     
$address->setDiscountDescription('Custom Discount');
     
$address->setBaseDiscountAmount(-$discountAmount);
     
$address->save();
    
}//end: if
   
//end: foreach
    
foreach($quote->getAllItems() as $item){
     
     
if($item->getBaseRowTotal()<$discountAmount{
     
     $item
->setDiscountAmount($item->getBaseRowTotal());
      
$item->setBaseDiscountAmount($item->getBaseRowTotal())
      ->
save();
      
     
$discountAmount=$discountAmount-$item->getBaseRowTotal();
     
}else {
      $item
->setDiscountAmount($discountAmount);
      
$item->setBaseDiscountAmount($discountAmount)
      ->
save();
      break;
    
}    
    
   } 
//end: foreach
   
}
  
 
 }
}

}

 
Magento Community Magento Community
Magento Community
Magento Community
 
gaeld
Jr. Member
 
Total Posts:  6
Joined:  2011-03-27
 

With this code you have somes issues with Tax Calculation.
And the calculation by product is not very precise.

Here is a updated code :

if($discountAmount>0{
    
   
//we calculate the Ratio of taxes between GrandTotal & Discount Amount to know how tach we need to remove.
   
$rat=1-($discountAmount/$quote->getGrandTotal());
   
$tax=$quote->getGrandTotal()-$quote->getSubtotal();
   
$tax=$tax*$rat;
   
$discountAmountWithoutTax=$discountAmount-$tax;
   
$total=$quote->getGrandTotal();
  
   
   
$quote->setGrandTotal($quote->getGrandTotal()-$discountAmount)
   ->
setBaseGrandTotal($quote->getBaseGrandTotal()-$discountAmount)
   ->
setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmountWithoutTax)
   ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmountWithoutTax)
   ->
save();
   
   
$canAddItems $quote->isVirtual()? ('billing') : ('shipping'); 
   foreach (
$quote->getAllAddresses() as $address{
    
    $address
->setSubtotal(0);
    
$address->setBaseSubtotal(0);
 
    
$address->setGrandTotal(0);
    
$address->setBaseGrandTotal(0);
 
    
$address->collectTotals();
    
    if(
$address->getAddressType()==$canAddItems{
 
     $address
->setSubtotal((float) $quote->getSubtotal());
     
$address->setBaseSubtotal((float) $quote->getBaseSubtotal());
     
$address->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount());
     
$address->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount());
     
$address->setGrandTotal((float) $quote->getGrandTotal());
     
$address->setBaseGrandTotal((float) $quote->getBaseGrandTotal());
     
$address->setDiscountAmount(-$discountAmount);
     
$address->setDiscountDescription($title);
     
$address->setBaseDiscountAmount(-$discountAmount);
     
$address->setTaxAmount($tax);
     
$address->setBaseTaxAmount($tax);
     
$address->save();
     
     
     
    
}//end: if
   
//end: foreach
    
foreach($quote->getAllItems() as $item){
     
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
     
$rat=$item->getBaseRowTotal()/$total;
     
$ratdisc=$discountAmount*$rat;
     
     
     
$item->setDiscountAmount($ratdisc);
     
$item->setBaseDiscountAmount($ratdisc);
    
    
   
//end: foreach
   
  
 
 
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Thanks

but i have one more problem,

in my case i have to add coupon code discount plus my custom discount at that time i am getting total and line items calculation is wrong if i use your code....but below is my code which work correctly but i dont know what will through error at the time of tax

this is my code

if($loyalty->getAmount()>0{
                $discountAmount
=$loyalty->getAmount();
            
                
                if(
$customer_group=='Employees'{
                
                    
foreach($quote->getAllItems() as $item){
                    
                        $discountAmount
=$discountAmount+$item->getDiscountAmount();    
                
//echo $discountAmount; exit;
                    
//end: foreach
                    
$discountdescription='Loyalty & Employee Discount';
                
}if($quote->getCouponCode()!='NULL'{
                
                    
foreach($quote->getAllItems() as $item){
                    
                        $discountAmount
=$discountAmount+$item->getDiscountAmount();    
                
//echo $discountAmount; exit;
                    
//end: foreach
                    
$discountdescription='Loyalty & '.$quote->getCouponCode();
                
}else {
                    $discountdescription
='Loyalty';
                
}
            
            $quote
->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
            ->
setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
            ->
setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
            ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
            ->
save();
            
            
$canAddItems $quote->isVirtual()? ('billing') : ('shipping');    
            foreach (
$quote->getAllAddresses() as $address{
                
                $address
->setSubtotal(0);
                
$address->setBaseSubtotal(0);
    
                
$address->setGrandTotal(0);
                
$address->setBaseGrandTotal(0);
    
            
//    $address->collectTotals();
                
                
if($address->getAddressType()==$canAddItems{
    
//echo $address->setDiscountAmount; exit;
                    
$address->setSubtotal((float) $quote->getSubtotal());
                    
$address->setBaseSubtotal((float) $quote->getBaseSubtotal());
                    
$address->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount());
                    
$address->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount());
                    
$address->setGrandTotal((float) $quote->getGrandTotal());
                    
$address->setBaseGrandTotal((float) $quote->getBaseGrandTotal());
                    
$address->setDiscountAmount(-$discountAmount);
                    
$address->setDiscountDescription($discountdescription);
                    
$address->setBaseDiscountAmount(-$discountAmount);
                    
$address->save();
                
}//end: if
            
//end: foreach
            
if($customer_group=='Employees' || $quote->getCouponCode()!='NULL'{
            $discountAmount2
=$loyalty->getAmount();
                foreach(
$quote->getAllItems() as $item){
                    
                    
if($item->getBaseRowTotal()<$discountAmount2{
                    
                    $item
->setDiscountAmount($item->getDiscountAmount()+$item->getBaseRowTotal());
                        
$item->setBaseDiscountAmount($item->getBaseDiscountAmount()+$item->getBaseRowTotal())
                        ->
save();
                        
                    
$discountAmount2=$discountAmount2-$item->getBaseRowTotal();
                    
}else {
                        $item
->setDiscountAmount($item->getDiscountAmount()+$discountAmount2);
                        
$item->setBaseDiscountAmount($item->getBaseDiscountAmount()+$discountAmount2)
                        ->
save();
                        break;
                
}                
                
                } 
//end: foreach            
            
}else {
                
foreach($quote->getAllItems() as $item){
                    
                    
if($item->getBaseRowTotal()<$discountAmount{
                    
                    $item
->setDiscountAmount($item->getBaseRowTotal());
                        
$item->setBaseDiscountAmount($item->getBaseRowTotal())
                        ->
save();
                        
                    
$discountAmount=$discountAmount-$item->getBaseRowTotal();
                    
}else {
                        $item
->setDiscountAmount($discountAmount);
                        
$item->setBaseDiscountAmount($discountAmount)
                        ->
save();
                        break;
                
}                
                
                } 
//end: foreach
            
}
            }

please tell how to over come from this issue

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Issue is fixed if you have applied multiple discount and also there will no issue in Tax Calculation. that is

both default coupon code and custom discount applied… use this code ( hear i use loyalty discount)

if($loyalty->getAmount()>0{
                $discountAmount
=$loyalty->getAmount();
                
$discountAmountLoyalty=$loyalty->getAmount();
            
                if(
$customer_group=='Employees'{
                    
foreach($quote->getAllItems() as $item){
                        $discountAmount
=$discountAmount+$item->getDiscountAmount();    
                    
//end: foreach
                    
$discountdescription='Loyalty & Employee Discount';
                
}elseif($quote->getCouponCode()!='NULL'{
                    
foreach($quote->getAllItems() as $item){
                        $discountAmount
=$discountAmount+$item->getDiscountAmount();    
                
                    
//end: foreach
                    
$discountdescription='Loyalty & '.$quote->getCouponCode();
                
}else {
                    $discountdescription
='Loyalty';
                
}
            
            
//we calculate the Ratio of taxes between GrandTotal & Discount Amount to know how tach we need to remove.
            
$total=$quote->getGrandTotal();
               
$quote->setGrandTotal($quote->getGrandTotal()-$discountAmountLoyalty)
               ->
setBaseGrandTotal($quote->getBaseGrandTotal()-$discountAmountLoyalty)
               ->
setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
               ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
               ->
save();
            
$canAddItems $quote->isVirtual()? ('billing') : ('shipping');    
            foreach (
$quote->getAllAddresses() as $address{
                
                $address
->setSubtotal(0);
                
$address->setBaseSubtotal(0);
    
                
$address->setGrandTotal(0);
                
$address->setBaseGrandTotal(0);
    
            
//    $address->collectTotals();
                
                
if($address->getAddressType()==$canAddItems{
    
//echo $address->setDiscountAmount; exit;
                    
$address->setSubtotal((float) $quote->getSubtotal());
                    
$address->setBaseSubtotal((float) $quote->getBaseSubtotal());
                    
$address->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount());
                    
$address->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount());
                    
$address->setGrandTotal((float) $quote->getGrandTotal());
                    
$address->setBaseGrandTotal((float) $quote->getBaseGrandTotal());
                    
$address->setDiscountAmount(-$discountAmount);
                    
$address->setDiscountDescription($discountdescription);
                    
$address->setBaseDiscountAmount(-$discountAmount);
                    
$address->save();
                
}//end: if
            
//end: foreach
            
            
$total=$quote->getGrandTotal()+$discountAmount;
            foreach(
$quote->getAllItems() as $item){
                 
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
                 
$rat=$item->getPriceInclTax()/$total;
                 
$ratdisc=$discountAmountLoyalty*$rat;
                          
                 
$item->setDiscountAmount($item->getDiscountAmount()+$ratdisc);
                 
$item->setBaseDiscountAmount($item->getBaseDiscountAmount()+$ratdisc)->save();
                
                
               
//end: foreach
               
            
}

And also for only custom discount is applied… use below code (both this code will be inside observer.php and call in event)

if($discountAmount>0{
            
              
//we calculate the Ratio of taxes between GrandTotal & Discount Amount to know how tach we need to remove.
        
               
$total=$quote->getGrandTotal();
              
               
               
$quote->setGrandTotal($quote->getGrandTotal()-$discountAmount)
               ->
setBaseGrandTotal($quote->getBaseGrandTotal()-$discountAmount)
               ->
setSubtotalWithDiscount($quote->getSubtotal()-$discountAmount)
               ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
               ->
save();
                        
            
$canAddItems $quote->isVirtual()? ('billing') : ('shipping');    
            foreach (
$quote->getAllAddresses() as $address{
                
                $address
->setSubtotal(0);
                
$address->setBaseSubtotal(0);
    
                
$address->setGrandTotal(0);
                
$address->setBaseGrandTotal(0);
    
                
$address->collectTotals();
                
                if(
$address->getAddressType()==$canAddItems{
    
                    $address
->setSubtotal((float) $quote->getSubtotal());
                    
$address->setBaseSubtotal((float) $quote->getBaseSubtotal());
                    
$address->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount());
                    
$address->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount());
                    
$address->setGrandTotal((float) $quote->getGrandTotal());
                    
$address->setBaseGrandTotal((float) $quote->getBaseGrandTotal());
                    
$address->setDiscountAmount(-$discountAmount);
                    
$address->setDiscountDescription('Refer Friend');
                    
$address->setBaseDiscountAmount(-$discountAmount);
                    
$address->save();
                
}//end: if
            
//end: foreach
                
foreach($quote->getAllItems() as $item){
                 
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
                 
$rat=$item->getPriceInclTax()/$total;
                 
$ratdisc=$discountAmount*$rat;
                 
                 
                 
$item->setDiscountAmount($ratdisc);
                 
$item->setBaseDiscountAmount($ratdisc)->save();
                
                
               
//end: foreach
            
            
}

File Attachments
Observer.php  (File Size: 22KB - Downloads: 62)
 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Hi gaeld,
actually if i use

->setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmountWithoutTax)
               ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmountWithoutTax)

subtotlawithdiscount calculation will be wrong because
SubtotalWithDiscount= Subtotal-Discount

so batter use

->setSubtotalWithDiscount($quote->getSubtotal()-$discountAmount)
               ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)

this will give correct magento calculation to0 test try for default magneto coupon code

and rest this is correct s you told that is

foreach($quote->getAllItems() as $item){
                 
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
                 
$rat=$item->getPriceInclTax()/$total;
                 
$ratdisc=$discountAmount*$rat;
                 
                 
                 
$item->setDiscountAmount($ratdisc);
                 
$item->setBaseDiscountAmount($ratdisc)->save();
                
                
               
//end: foreach

in this post i added correct code for both only custom discount and also both default coupon and custom discount please check it

,

gaeld - 22 December 2011 05:43 AM

With this code you have somes issues with Tax Calculation.
And the calculation by product is not very precise.

Here is a updated code :

if($discountAmount>0{
    
   
//we calculate the Ratio of taxes between GrandTotal & Discount Amount to know how tach we need to remove.
   
$rat=1-($discountAmount/$quote->getGrandTotal());
   
$tax=$quote->getGrandTotal()-$quote->getSubtotal();
   
$tax=$tax*$rat;
   
$discountAmountWithoutTax=$discountAmount-$tax;
   
$total=$quote->getGrandTotal();
  
   
   
$quote->setGrandTotal($quote->getGrandTotal()-$discountAmount)
   ->
setBaseGrandTotal($quote->getBaseGrandTotal()-$discountAmount)
   ->
setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmountWithoutTax)
   ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmountWithoutTax)
   ->
save();
   
   
$canAddItems $quote->isVirtual()? ('billing') : ('shipping'); 
   foreach (
$quote->getAllAddresses() as $address{
    
    $address
->setSubtotal(0);
    
$address->setBaseSubtotal(0);
 
    
$address->setGrandTotal(0);
    
$address->setBaseGrandTotal(0);
 
    
$address->collectTotals();
    
    if(
$address->getAddressType()==$canAddItems{
 
     $address
->setSubtotal((float) $quote->getSubtotal());
     
$address->setBaseSubtotal((float) $quote->getBaseSubtotal());
     
$address->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount());
     
$address->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount());
     
$address->setGrandTotal((float) $quote->getGrandTotal());
     
$address->setBaseGrandTotal((float) $quote->getBaseGrandTotal());
     
$address->setDiscountAmount(-$discountAmount);
     
$address->setDiscountDescription($title);
     
$address->setBaseDiscountAmount(-$discountAmount);
     
$address->setTaxAmount($tax);
     
$address->setBaseTaxAmount($tax);
     
$address->save();
     
     
     
    
}//end: if
   
//end: foreach
    
foreach($quote->getAllItems() as $item){
     
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
     
$rat=$item->getBaseRowTotal()/$total;
     
$ratdisc=$discountAmount*$rat;
     
     
     
$item->setDiscountAmount($ratdisc);
     
$item->setBaseDiscountAmount($ratdisc);
    
    
   
//end: foreach
   
  
 
 
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Hi,

Finally I did lot of research and code to fine code for discount in magento

in your custom module etc/config.xml add this code in side global tag

<events
   <
sales_quote_collect_totals_after>
                <
observers>
                    <
set_discount_static>
                        <
type>singleton</type>
                        <class>
Loyalty_Loyalty_Model_Observer</class>
                        <
method>setdiscountamount</method>
                    </
set_discount_static>
                </
observers>
            </
sales_quote_collect_totals_after>  
   
   </
events
and inside model folder create file called Observer.php
if($discountAmount>0{
                
            
                    $discountdescription
='custom discount';
                
            
            
//we calculate the Ratio of taxes between GrandTotal & Discount Amount to know how tach we need to remove.
            
$total=$quote->getGrandTotal();
               
$quote->setGrandTotal($quote->getGrandTotal()-$discountAmount)
               ->
setBaseGrandTotal($quote->getBaseGrandTotal()-$discountAmount)
               ->
setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
               ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
               ->
save();
            
$canAddItems $quote->isVirtual()? ('billing') : ('shipping');    
            foreach (
$quote->getAllAddresses() as $address{
                
                $address
->setSubtotal(0);
                
$address->setBaseSubtotal(0);
    
                
$address->setGrandTotal(0);
                
$address->setBaseGrandTotal(0);
    
            
//    $address->collectTotals();
                
                
if($address->getAddressType()==$canAddItems{
    
//echo $address->setDiscountAmount; exit;
                    
$address->setSubtotal((float) $quote->getSubtotal());
                    
$address->setBaseSubtotal((float) $quote->getBaseSubtotal());
                    
$address->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount());
                    
$address->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount());
                    
$address->setGrandTotal((float) $quote->getGrandTotal());
                    
$address->setBaseGrandTotal((float) $quote->getBaseGrandTotal());
                    
$address->setDiscountAmount(-$discountAmount);
                    if(
$address->getDiscountDescription()){
                    $address
->setDiscountDescription($address->getDiscountDescription().','.$discountdescription);
                    
}else {
                    $address
->setDiscountDescription($discountdescription);
                    
}
                    $address
->setBaseDiscountAmount(-$discountAmount);
                    
$address->save();
                
}//end: if
            
//end: foreach
            
            
$total=$quote->getGrandTotal()+$discountAmount;
            foreach(
$quote->getAllItems() as $item){
                 
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
                 
$rat=$item->getPriceInclTax()/$total;
                 
$ratdisc=$discountAmount*$rat;
                          
                 
$item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
                 
$item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
                
                
               
//end: foreach
               
            
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
nlampi
Jr. Member
 
Total Posts:  3
Joined:  2011-09-15
 

Thanks for posting this update Pradeep, it helped me out a lot.

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Hi,
i am updating latest observer file for custom discount

<?php
/**
 * @category   Sugarcode
 * @package    Sugarcode_Customdiscount
 * @author     pradeep.kumarrcs67@gmail.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class Sugarcode_Customdiscount_Model_Observer
{

    
 
public function setDiscount($observer)
    
{
       $quote
=$observer->getEvent()->getQuote();
       
$quoteid=$quote->getId();
       
$discountAmount=10;
    if(
$quoteid{
       
       
      
        
if($discountAmount>0{
        $total
=$quote->getBaseSubtotal();
            
$quote->setSubtotal(0);
            
$quote->setBaseSubtotal(0);

            
$quote->setSubtotalWithDiscount(0);
            
$quote->setBaseSubtotalWithDiscount(0);

            
$quote->setGrandTotal(0);
            
$quote->setBaseGrandTotal(0);
        
             
            
$canAddItems $quote->isVirtual()? ('billing') : ('shipping');    
            foreach (
$quote->getAllAddresses() as $address{
                
            $address
->setSubtotal(0);
            
$address->setBaseSubtotal(0);

            
$address->setGrandTotal(0);
            
$address->setBaseGrandTotal(0);

            
$address->collectTotals();

            
$quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
            
$quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());

            
$quote->setSubtotalWithDiscount(
                (float) 
$quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
            );
            
$quote->setBaseSubtotalWithDiscount(
                (float) 
$quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
            );

            
$quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
            
$quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
    
            
$quote ->save(); 
    
               
$quote->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
               ->
setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
               ->
setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
               ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
               ->
save(); 
               
                
                if(
$address->getAddressType()==$canAddItems{
                
//echo $address->setDiscountAmount; exit;
                    
$address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount()-$discountAmount);
                    
$address->setGrandTotal((float) $address->getGrandTotal()-$discountAmount);
                    
$address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount()-$discountAmount);
                    
$address->setBaseGrandTotal((float) $address->getBaseGrandTotal()-$discountAmount);
                    if(
$address->getDiscountDescription()){
                    $address
->setDiscountAmount(-($address->getDiscountAmount()-$discountAmount));
                    
$address->setDiscountDescription($address->getDiscountDescription().', Custom Discount');
                    
$address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
                    
}else {
                    $address
->setDiscountAmount(-($discountAmount));
                    
$address->setDiscountDescription('Custom Discount');
                    
$address->setBaseDiscountAmount(-($discountAmount));
                    
}
                    $address
->save();
                
}//end: if
            
//end: foreach
            //echo $quote->getGrandTotal();
        
        
foreach($quote->getAllItems() as $item){
                 
//We apply discount amount based on the ratio between the GrandTotal and the RowTotal
                 
$rat=$item->getPriceInclTax()/$total;
                 
$ratdisc=$discountAmount*$rat;
                 
$item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
                 
$item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
                
               
}
            
                
            }
            
    }
 }

}

File Attachments
Observer.php  (File Size: 4KB - Downloads: 89)
 
Magento Community Magento Community
Magento Community
Magento Community
 
keanokia
Jr. Member
 
Avatar
Total Posts:  23
Joined:  2012-10-17
 

your a fucking awesome.. it works, thanks pradeep_kumar67

 
Magento Community Magento Community
Magento Community
Magento Community
 
justin00
Jr. Member
 
Total Posts:  28
Joined:  2009-02-20
 

hi.... i managed to install this extension by the above instruction, i can see the module in the backend sugardcode_customdiscount, but how & where can i set the custom discount to customer’s orders?

Best regards,
Justin

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 
justin00 - 10 April 2013 06:22 AM

hi.... i managed to install this extension by the above instruction, i can see the module in the backend sugardcode_customdiscount, but how & where can i set the custom discount to customer’s orders?

Best regards,
Justin

$discountAmount=10;

is where it set discount

when ever product in cart it will calculate the total at that time this event will triggered and set discount

now you have to write condition according to your requirement and you can extend the module and set discount price from admin configuration only which can set by admin

 
Magento Community Magento Community
Magento Community
Magento Community
 
sukhwantc
Jr. Member
 
Total Posts:  29
Joined:  2013-04-17
 

Thanks Man. It works great, helped me.

Thanks again

regards
Sukhwant

 
Magento Community Magento Community
Magento Community
Magento Community
 
sukhwantc
Jr. Member
 
Total Posts:  29
Joined:  2013-04-17
 

Its not working on the checkout page. Discount is applied on the cart page and displayed but its not reverting on checkout and after placing order no discount is applied. Any suggestions or any event I need to observe to make it work automatically on checkout page also.

regards
Sukhwant

 
Magento Community Magento Community
Magento Community
Magento Community
 
bannostookaylo
Jr. Member
 
Avatar
Total Posts:  9
Joined:  2013-04-04
 

I can’t get this to work on checkout page either… I am trying to recall it within the observer checkout_controller_onepage_save_shipping_method but its not showing the payment option of “Zero Subtotal Checkout” any suggestions on what to do here? I’ve hard coded the script to zero everything out so I know for a fact the ending result is 0

 
Magento Community Magento Community
Magento Community
Magento Community
 
bannostookaylo
Jr. Member
 
Avatar
Total Posts:  9
Joined:  2013-04-04
 

From what I can tell, and correct me if I am wrong. This is not taking into account for the shipping amount that maybe included in the fee… $quote->getShippingAddress()->getShippingAmount() ... shouldn’t the shipping amount be applied to the Grand Total?

 
Magento Community Magento Community
Magento Community
Magento Community
 
bannostookaylo
Jr. Member
 
Avatar
Total Posts:  9
Joined:  2013-04-04
 

Well crap now I can’t get the “Zero Subtotal Checkout” option to go away LOL grrrrr magento I modified to your code to include this

$ShippingAmount $quote->getShippingAddress()->getShippingAmount();
                            
$BaseShippingAmount $quote->getShippingAddress()->getBaseShippingAmount();
                            
                            
$quote->setGrandTotal($quote->getBaseSubtotal() - $discountAmount $ShippingAmount)
                            ->
setBaseGrandTotal($quote->getBaseSubtotal() - $discountAmount $BaseShippingAmount)
                            ->
setSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount $ShippingAmount)
                            ->
setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount $BaseShippingAmount)
                            ->
save();

but im not sure if that is a good idea or even correct.. im kind of winging it here lol

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 2