Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8885201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:15:02+00:00 2026-06-14T21:15:02+00:00

I have created a surcharge module for Magento which adds a custom total field

  • 0

I have created a surcharge module for Magento which adds a custom total field to the quote. The surcharge is input into Magento including tax. I have successfully got the module adding the surcharge to the quote and the grand total is correct on the checkout page.

My issue comes when trying to apply tax to the surcharge so that it gets included and displayed in the tax field on the checkout page. Currently it only includes the tax for the products and the shipping.

I have managed to calculate the tax for the surcharge but cant’t get the tax applied to the quote so that it is displayed in the tax field but also don’t think my approach is quite correct either. Any help would be much appreciated.

class ********_Deposits_Model_Quote_Address_Total_Surcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract {

 protected $_config = null;

 public function __construct()
 {
    $this->setCode('surcharge_price');
    $this->_config      = Mage::getSingleton('tax/config');
    $this->_store = Mage::app()->getStore();
 }

 protected function _calculateTax(Mage_Sales_Model_Quote_Address $address)
{
    $calculator     = Mage::getSingleton('tax/calculation');
    $inclTax        = $this->_config->priceIncludesTax($this->_store);

    $taxRateRequest = $calculator->getRateRequest(
        $address,
        $address->getQuote()->getBillingAddress(),
        $address->getQuote()->getCustomerTaxClassId(),
        $this->_store
    );

    $taxRateRequest->setProductClassId(Mage::getStoreConfig('********/surcharges/tax_class', $this->_store));

    $rate = $calculator->getRate($taxRateRequest);

    $baseTax = $tax = $calculator->calcTaxAmount($address->getBaseSurchargePriceAmount(), $rate, $inclTax, true);


}

/**
 * Collect address subtotal
 *
 * @param   ********_Surcharges_Model_Quote_Address $address
 * @return  ********_Surcharges_Model_Quote_Address_Total_Surcharge
 */
public function collect(Mage_Sales_Model_Quote_Address $address)
{

    parent::collect($address);

    $this->_setAmount(0)->_setBaseAmount(0);

    // If Surcharges Is Enabled Then Calculate Away :-)
    if(Mage::getStoreConfig('********/surcharges/surcharge_enabled')) {

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

      // Calculate Total Surcharge For Items In Quote (Base Prices!)
      $surcharge = 0.0;
      foreach ($items as $item) {
        $price = $item->getData('base_surcharge_price', null);
        if(isset($price)) {
          $surcharge += $item->getData('base_surcharge_price') * $item->getQty();
        }
      }

     $this->_setAmount($surcharge);
     $this->_setBaseAmount($surcharge);

     $this->_calculateTax($address);
    }


    return $this;
}

public function fetch(Mage_Sales_Model_Quote_Address $address)
{

  if(Mage::getStoreConfig('********/surcharges/surcharge_enabled')) {
    $surcharge = $address->getSurchargePriceAmount();
    if(isset($surcharge) && $surcharge > 0) {
      $address->addTotal(array(
          'code'  => $this->getCode(),
          'title' => Mage::getStoreConfig('********/surcharges/surcharge_label'),
          'value' => $surcharge
      ));
    }
  }

  return $this;
}

public function getLabel()
{
    return Mage::getStoreConfig('********/surcharges/surcharge_label');
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:15:03+00:00Added an answer on June 14, 2026 at 9:15 pm

    I managed to solve this this using the following code. Not the best solution but spent hours trying to do it and didn’t get anywhere fast. Asterix’s need to be replaced.

    class *****_Deposits_Model_Quote_Address_Total_Surcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract {
    
     protected $_taxConfig = null;
    
     public function __construct()
     {
        $this->setCode('surcharge_price');
        $this->_taxConfig      = Mage::getSingleton('tax/config');
        $this->_store = Mage::app()->getStore();
     }
    
     protected function _calculateTax(Mage_Sales_Model_Quote_Address $address)
    {
        $calculator     = Mage::getSingleton('tax/calculation');
        $calculator->setCustomer($address->getQuote()->getCustomer());
    
        $inclTax        = $this->_taxConfig->priceIncludesTax($this->_store);
    
        $taxRateRequest = $calculator->getRateRequest(
            $address,
            $address->getQuote()->getBillingAddress(),
            $address->getQuote()->getCustomerTaxClassId(),
            $this->_store
        );
    
        $taxRateRequest->setProductClassId(Mage::getStoreConfig('*****/surcharges/tax_class', $this->_store));
    
        $rate = $calculator->getRate($taxRateRequest);
    
        if($rate > 0.0) {
          $baseTax = $calculator->calcTaxAmount($address->getBaseSurchargePriceAmount(), $rate, $inclTax, true);
          $tax = $address->getQuote()->getStore()->convertPrice($baseTax, false);
    
          $address->addTotalAmount('tax', $tax);
          $address->addBaseTotalAmount('tax', $baseTax);
    
          $rates = array();
          foreach ($address->getAppliedTaxes() as $rate) {
            $rate['amount'] = $rate['amount'] + $tax;
            $rate['base_amount'] = $rate['base_amount'] + $baseTax;
            $rates[] = $rate;
          }
    
          $address->setAppliedTaxes($rates);
    
          if($inclTax) {
            $address->setGrandTotal($address->getGrandTotal() - $tax);
            $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseTax);
          }
        }
    
    }
    
    /**
     * Collect address subtotal
     *
     * @param   *****_Surcharges_Model_Quote_Address $address
     * @return  *****_Surcharges_Model_Quote_Address_Total_Surcharge
     */
    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
    
        parent::collect($address);
    
        // Clear Cached Values As Multiple Addresses Causes Values To Be Added Twice Otherwise!
        $this->_setAmount(0)->_setBaseAmount(0);
    
        // If Surcharges Is Enabled Then Calculate Away :-)
        if(Mage::getStoreConfig('*****/surcharges/surcharge_enabled')) {
    
          $items = $this->_getAddressItems($address);
          if (!count($items)) {
              return $this;
          }
    
          // Calculate Total Surcharge For Items In Quote (Base Prices!)
          $surcharge = 0.0;
          foreach ($items as $item) {
            $price = $item->getData('base_surcharge_price', null);
            if(isset($price)) {
              $surcharge += $item->getData('base_surcharge_price') * $item->getQty();
            }
          }
    
         $this->_setAmount($address->getQuote()->getStore()->convertPrice($surcharge, false));
         $this->_setBaseAmount($surcharge);
    
          $this->_calculateTax($address);
        }
    
    
        return $this;
    }
    
    /**
     * Assign subtotal amount and label to address object
     *
     * @param   *****_Surcharges_Model_Quote_Address $address
     * @return  *****_Surcharges_Model_Quote_Address_Total_Surcharge
     */
    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
    
      if(Mage::getStoreConfig('*****/surcharges/surcharge_enabled')) {
        $surcharge = $address->getSurchargePriceAmount();
        if(isset($surcharge) && $surcharge > 0) {
          $address->addTotal(array(
              'code'  => $this->getCode(),
              'title' => Mage::getStoreConfig('*****/surcharges/surcharge_label'),
              'value' => $surcharge
          ));
        }
      }
    
      return $this;
    }
    
    /**
     * Get Surcharge label
     *
     * @return string
     */
    public function getLabel()
    {
        return Mage::getStoreConfig('*****/surcharges/surcharge_label');
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a module in Magento which takes certain views from customers about
i have created a running process which listens for input: listen = Popen([home/user/listen], stdout=PIPE,
I have created a UICollectionViewController custom class , and I inserted an UIImageView into
I have created a custom post type named People. I have created a page
I have created form using ModelForm but its not saving data into database. views.py
We have created a .NET 4 web service, which runs fine on a Windows
Have created a ATL COM project through which I am inserting Menu Items to
Have created a custom navigation menu in wordpress that has some pages and some
I have created custom listview in that have list of textview & list of
I have created a bar chart which is a improvement of bar graph on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.