4

I would like to manipulate the Shipping and Handling fee during the checkout process. Unfortunately, I cannot set an additional fee in the shipping method config because it's applied on a per-customer basis.

I have created a simple true/false customer attribute to determine if a customer should pay an extra shipping fee; if true, I want to add $5 to their shipping/handling.

I can add extra line items to the total by creating models like Model_Quote_Address_Totals_NewLineItem, but I cannot figure out how to edit the shipping fee directly.

What is the best way to modify the shipping/handling fee during checkout?

9

There isn't a clean interface to do that, but the following hack works well in any Magento version.
First declare an observer for the sales_quote_collect_totals_before event.

<config>
    <global>
        <events>
            <sales_quote_collect_totals_before>
                <observers>
                    <your_module>
                        <type>singleton</type>
                        <class>your_module/observer</class>
                        <method>salesQuoteCollectTotalsBefore</method>
                    </your_module>
                </observers>
            </sales_quote_collect_totals_before>
        </events>
    </global>
</config>

Then, in the observer, adjust the handling type and fee configuration for each carrier in the corresponding store model.

public function salesQuoteCollectTotalsBefore(Varien_Event_Observer $observer)
{
    /** @var Mage_Sales_Model_Quote $quote */
    $quote = $observer->getQuote();

    // Check your customer attribute of choice here instead of the customer id
    if ($quote->getCustomer()->getId()) {

        $store = Mage::app()->getStore($quote->getStoreId());
        $carriers = Mage::getStoreConfig('carriers', $store);
        foreach ($carriers as $carrierCode => $carrierConfig) {

            // F for fixed, P for percentage
            $store->setConfig("carriers/{$carrierCode}/handling_type", 'F');

            // 0 for no fee, otherwise fixed of percentage value
            $store->setConfig("carriers/{$carrierCode}/handling_fee", '1');
        }
    }
}

Note that the configuration changes are not saved, they are only valid during the request. But because they are applied before the totals are collected, they will be taken into account whenever the shipping rates are calculated.

| improve this answer | |

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.