B-219 Sec-55 Noida, India
+918010221733

Adding Surcharges in Magento

Recently I needed to add a surcharge to orders using a particular payment type. Surcharges are what is called a “Total”. They need to be created in the same way as Delivery, Discounts, etc. This post assumes knowledge of module creation.

Please note that this post is incomplete! I’m pressed for time and I’ll be back to edit this with more detailed information on WHY we do it.

So first off, create a new Module for Surcharges in magento/app/code/local/<namespace>/Surcharges/ with the usual subfolders. Don’t forget to create the module’s xml file in /app/etc/modules/.

in <namespace>/Surcharges/etc/config.xml:

<?php
    <config>
        …
        <global>
            …
            <sales>
                <quote>
                    <totals>
                        <surcharge>
                            <class>surcharges/quote_address_total_surcharge</class>
                            <after>tax</after>
                        </surcharge>
                    </totals>
                </quote>
            </sales>
            <fieldsets>
                <sales_convert_quote>
                    <surcharge><to_order>*</to_order></surcharge>
                    <surcharge_tax><to_order>*</to_order></surcharge_tax>
                </sales_convert_quote>
            </fieldsets>
            …
        </global>
        …
    </config>
?>

in <namespace>/Surcharges/sql/surcharges_setup/mysql4-install-0.1.0.php

<?php

    $installer = $this;

    $installer->startSetup();

    $installer->addAttribute(
        ‘order’,
        ‘surcharge’,
        array(
            ‘type’ => ‘float’,
            ‘grid’ => false
        )
    );
    $installer->addAttribute(
        ‘order’,
        ‘surcharge_tax’,
        array(
            ‘type’ => ‘float’,
            ‘grid’ => false
        )
    );

    $installer->addAttribute(
        ‘quote’,
        ‘surcharge’,
        array(
            ‘type’ => ‘float’,
            ‘grid’ => false
        )
    );
    $installer->addAttribute(
        ‘quote’,
        ‘surcharge_tax’,
        array(
            ‘type’ => ‘float’,
            ‘grid’ => false
        )
    );

    $installer->endSetup();

in <namespace>/Surcharges/Model/Quote/Address/Total/Surcharge.php:

<?php

class {{namespace}}_Surcharges_Model_Quote_Address_Total_Surcharge
    extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
   
    public function __construct()
    {
        $this->setCode(‘surcharge’);
    }

    public function collect(Mage_Sales_Model_Quote_Address $address)
    {       
        $amount = $address->getShippingAmount();
        if ($amount != 0 || $address->getShippingDescription()) {
                       
            $address->setSurchargeAmount($this->getSurchargeAmount());
            $address->setSurchargeTaxAmount($this->getSurchargeTaxAmount());
           
            $address->setSurcharge($this->getSurchargeAmount());
            $address->getQuote()->setData(‘surcharge’, $this->getSurchargeAmount());
            $address->getQuote()->setData(‘surcharge_tax’, $this->getSurchargeTaxAmount());
           
            $address->setTaxAmount($address->getTaxAmount() + $address->getSurchargeTaxAmount());
            $address->setBaseTaxAmount(
                $address->getBaseTaxAmount() + $address->getSurchargeTaxAmount()
            );
            $address->setSubtotal($address->getSubtotal() + $this->getSurchargeAmount(true));
            $address->setBaseSubtotal(
                $address->getBaseSubtotal() + $this->getSurchargeAmount(true)
            );
            $address->setGrandTotal($address->getGrandTotal() + $address->getSurchargeAmount());
            $address->setBaseGrandTotal(
                $address->getBaseGrandTotal() + $address->getSurchargeAmount()
            );
           
        }
        return $this;
    }
   
    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        $amount = $address->getShippingAmount();
        if ($amount != 0 || $address->getShippingDescription()) {
            if ($address->getSurchargeAmount()) {
                $address->addTotal(array(
                    ‘code’  => $this->getCode(),
                    ‘title’ => $this->getSurchargeTitle(),
                    ‘value’ => $address->getSurchargeAmount()
                ));
            }
        }
        return $this;
    }
   
    public function getSurchargeTitle()
    {
        $title = “Surcharges”;
        return $title;
    }
   
    public function getSurchargeAmount()
    {
        $amount = (float)10;
        return $amount;
    }
   
    public function getSurchargeTaxAmount()
    {
        $taxpercent = (float)10;
        $tax = ($this->getSurchargeAmount()/100)*$taxpercent;
        return $tax;
    }
   
}

(Visited 83 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.