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:
- Different shipping price based on the shipping address
- Different shipping price based on weight
- Different shipping price based on categories
- Different shipping price based on no of items
- Different shipping price based on your custom module
- 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!
Thank for the post!
Magento shipping can be a real pain. I’ve tried different methods and now I’m using a combination of extensions to calculate shipping costs. For example, this free extension https://amasty.com/magento-auto-shipping.html help calculate shipping costs for default address.
Hi, I need to set a fixed price for USPS Priority Mail shipping and I am not being able. Anyone can help?
I created the two files mentioned in the article but nothing happens.
I also had to create directory Shipmentfilter because I didn’t have it in my files.
Thanks!
Thanks for the post.. I’am having the same requirement for Magento 2, Could you please help me to how to change shipping price for M2. Thank you.
your help would much appreciated..