8

I want to add some extra amount to Grand total in Magento. So in checkout page, order review section will look like this:

enter image description here This extra charges will depends on some conditions.

My question is: How can I change the grand total in checkout page? For this what I did is: I create a module. Please look my code:

app/code/local/Locwiseship/Customprice/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Locwiseship_Customprice>
            <version>1.0.10</version>
        </Locwiseship_Customprice>
    </modules>
    <global>
        <events>
            <!-- Création éventuelle du lien de parrainage lors de la commande -->
            <sales_quote_collect_totals_after>
                <observers>
                    <set_custom_price_locwiseship>
                        <type>singleton</type>
                        <class>Locwiseship_Customprice_Model_Sales_Quote_Address_Total_Mytotal</class>
                        <method>collect</method>
                        <method>fetch</method>
                    </set_custom_price_locwiseship>
                </observers>
            </sales_quote_collect_totals_after>
        </events>
    </global>
</config>

app/code/local/Locwiseship/Customprice/Model/Observer.php

<?php

/**
 * @category   Locwiseship
 * @package    Locwiseship_Customprice
 */
class Locwiseship_Customprice_Model_Sales_Quote_Address_Total_Mytotal
    extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
    public function __construct()
    {
        $this->setCode('mytotal');
    }

    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        parent::collect($address);

        foreach ($this->_getAddressItems($address) as $item) {
            // These two lines represent whatever logic you're 
            // using to calculate these amounts
            $baseAmt = 10;
            $amt = 10;

            // Set the item's total
            $item->setBaseMytotalAmount($baseAmt);
            $item->setMytotalAmount($amt);

            // These methods automatically take care of summing 
            // "mytotal_amount" on the quote address
            $this->_addBaseAmount($baseAmt);
            $this->_addAmount($amt);
        }
        return $this;
    }

    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        // Naturally, this exists on the quote address because "collect" ran already
        $amt = $address->getMytotalAmount();

        if ($amt != 0) {
            $address->addTotal(array(
                'code' => $this->getCode(),
                'title' => Mage::helper('Locwiseship_Customprice')->__('My Total'),
                'value' => $amt
            ));
        }
        return $this;
    }
}

app/etc/modules/Locwiseship_Customprice.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Locwiseship_Customprice>
            <active>true</active>
            <codePool>local</codePool>
        </Locwiseship_Customprice>
    </modules>
</config>

Thais is my code. But Nothing happened..This is not working.. Is this the correct approach? How can I make this working? Please help me.. I am using Magento 1.9.0.1

1

I would not recomend doing this with an observer. You should add this new total as a new column to sales_flat_quote and sales_flat_order (you can also add this new column to invoice and creditmemo).

This is what I did when I needed to add an extra total to display customers credit. In your config.xml you need to add this:

<blocks>
    <customercredit>
        <class>Company_Customercredit_Block</class>
    </customercredit>

    <adminhtml>
        <rewrite>
            <sales_order_totals>Company_Customercredit_Block_Adminhtml_Sales_Order</sales_order_totals>
            <sales_order_invoice_totals>Company_Customercredit_Block_Adminhtml_Sales_Invoice</sales_order_invoice_totals>
            <sales_order_creditmemo_totals>Company_Customercredit_Block_Adminhtml_Sales_Creditmemo</sales_order_creditmemo_totals>
        </rewrite>
    </adminhtml>

    <sales>
        <rewrite>
            <order_totals>Company_Customercredit_Block_Sales_Order</order_totals>
            <order_invoice_totals>Company_Customercredit_Block_Sales_Invoice</order_invoice_totals>
            <order_creditmemo_totals>Company_Customercredit_Block_Sales_Creditmemo</order_creditmemo_totals>
        </rewrite>
    </sales>
</blocks>

<fieldsets>
    <sales_convert_quote_address>
        <base_customer_credit>
            <to_order>*</to_order>
        </base_customer_credit>
        <customer_credit>
            <to_order>*</to_order>
        </customer_credit>
    </sales_convert_quote_address>
</fieldsets>

<sales>
    <quote>
        <totals>
            <customer_credit>
                <class>Company_Customercredit_Model_Sales_Credit</class>
                <after>subtotal,discount,shipping,tax</after>
                <before>grand_total</before>
            </customer_credit>
        </totals>
    </quote>

    <order_invoice>
        <totals>
            <customer_credit>
                <class>Company_Customercredit_Model_Sales_Invoice</class>
                <after>subtotal</after>
            </customer_credit>
        </totals>
    </order_invoice>

    <order_creditmemo>
        <totals>
            <customer_credit>
                <class>Company_Customercredit_Model_Sales_Creditmemo</class>
                <after>subtotal</after>
            </customer_credit>
        </totals>
    </order_creditmemo>
</sales>

Then your block classes should look like this:

class Company_Customercredit_Block_Adminhtml_Sales_Order extends Mage_Sales_Block_Order_Totals
{
    protected $_code = 'credit';

    protected function _initTotals()
    {
        $helper = $this->getCreditsHelper();
        parent::_initTotals();
        $creditAmount = $this->getOrder()->getCustomerCredit();
        $baseAmount = $this->getOrder()->getBaseCustomerCredit();

        if ($creditAmount != 0)
        {
            $this->addTotal(
                new Varien_Object(
                    array(
                        'code' => $this->_code,
                        'value' => -$creditAmount,
                        'base_value' => -$baseAmount,
                        'label' => $helper->__('Company Credit'),
                    )
                ));
        }
        return $this;
    }

    /**
     * Get credits helper.
     * @return Company_Customercredits_Helper_Data
     */
    protected function getCreditsHelper()
    {
        return Mage::helper('customercredit');
    }

}

Here you have some nice links with a more detail explanaition: http://www.classyllama.com/blog/unravelling-magentos-collecttotals-example http://astrio.net/blog/magento-development-add-total-row-checkout/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.