Change shipping price & handling fee on the fly in Magento

Problem: How do I change shipping prices and handling fees on the fly?

You may want to have different shipping prices for different scenarios. Your scenario could be any of the following:

  1. Different shipping price based on the shipping address
  2. Different shipping price based on weight
  3. Different shipping price based on categories
  4. Different shipping price based on no of items
  5. Different shipping price based on your custom module
  6. Different shipping price based on some conditions

There may be many solutions. A flexible solution which works for most cases is to develop a custom module which implements the event called ‘sales_quote_collect_totals_before’

Solution:

Assuming the skeleton module: MagePsycho_Shipmentfilter has already been created.
1. Register the event ‘sales_quote_collect_totals_before’:
Add the following XML code in app/code/local/MagePsycho/Shipmentfilter/etc/config.xml


...
<events>
    <sales_quote_collect_totals_before>
        <observers>
            <your_module>
                <type>singleton</type>
                <class>shipmentfilter/observer</class>
                <method>salesQuoteCollectTotalsBefore</method>
            </your_module>
        </observers>
    </sales_quote_collect_totals_before>
</events>
...

2. Implement the observer model:
Create the observer file: app/code/local/MagePsycho/Shipmentfilter/Model/Observer.php and add the following code:


class MagePsycho_Shipmentfilter_Model_Observer
{
    public function salesQuoteCollectTotalsBefore(Varien_Event_Observer $observer)
    {
        /** @var Mage_Sales_Model_Quote $quote */
        $quote = $observer->getQuote();
        $someConditions = true; //this can be any condition based on your requirements
        $newHandlingFee = 15;
        $store    = Mage::app()->getStore($quote->getStoreId());
        $carriers = Mage::getStoreConfig('carriers', $store);
        foreach ($carriers as $carrierCode => $carrierConfig) {
            if($carrierCode == 'fedex'){
                if($someConditions){
                    Mage::log('Handling Fee(Before):' . $store->getConfig("carriers/{$carrierCode}/handling_fee"), null, 'shipping-price.log');
                    $store->setConfig("carriers/{$carrierCode}/handling_type", 'F'); #F - Fixed, P - Percentage                  
                    $store->setConfig("carriers/{$carrierCode}/handling_fee", $newHandlingFee);
    
                    ###If you want to set the price instead of handling fee you can simply use as:
                    #$store->setConfig("carriers/{$carrierCode}/price", $newPrice);
    
                    Mage::log('Handling Fee(After):' . $store->getConfig("carriers/{$carrierCode}/handling_fee"), null, 'shipping-price.log');
                }
            }
        }
    }
}

Notes: Here we are adding a handling fee of 15 for FedEx shipping carrier on some conditions.
If you want to set a new price instead of a handling fee then you can simply use:

$store->setConfig("carriers/{$carrierCode}/price", $newPrice);

in the above code.
Similarly, you can add any custom shipping price for any other shipping method.

3. Bingo!