Magento Certified Developer GuideCategory: 8. CheckoutHow you can customize total models
Manish Prakash Staff asked 2 years ago

Totals Models are defined in config.xml file e.g if you open sales/config.xml you will see many totals module defined there

<global>
   <quote>
      <totals>
             .....
      </totals>
   </quote>
</global>

similarly totals for order_invoice, creditmemo etc are defined there.
All total model classes should extend Mage_Sales_Model_Quote_Address_Total_Abstract
and class should have a code and define “fetch()” , “collect()” function

class Module_Model_Quote_Address_Total_Custom extends Mage_Sales_Model_Quote_Address_Total_Abstract{
  
   public function __construct(){
        $this->setCode('custom');
    }

  public function collect($address){
   //this function is used add/subtract price from order totals  

   $items = $address->getAllItems();
        if (!count($items)) {
            return $this;
        }

    //above code basically works only for billing address, if this code is not added your total will add up 2 times
    return $address;
  }


  public function fetch($address){
     //this class is used for display purposes
    $address->addTotal(array(
                'code'=>$this->getCode(),
                'title'=>Mage::helper('sales')->__('Custom'),
                'value'=>$amount
            ));
return $address;
  }

 
}

To customize a total’s model you can either override if from config.xml or create a new one as described above.