Add payment method handling cost
Posted on August 9th, 2009, by Cristian in MagentoThis week I worked at a Magento downpayment payment module which required a way to add handling fee. My problem was that the fee should be added to checkout total once the payment method was check. After looking at Magento API I come with a solution.
The order has a method setShippingAmount and a method getShippingAmount and I tried to use them. I adjust the total shipping amount by increading with my module handling fee.
$shipping_amount = $order->getShippingAmount(); $order->setShippingAmount( $shipping_amount + $mine_handling_amount );
The way that I did it was to create a method updateShippingAmount and add it as observe for checkout_type_onepage_save_order event in the module/etc/config.xml file:
public function updateShippingAmount( $observer )
{
$MyPaymentMethod = Mage::getSingleton('namespace/mypaymentmethod');
$order = $observer->getEvent()->getOrder();
$payment = $order->getPayment()->getData();
if( $payment['method'] == $MyPaymentMethod->getCode() )
{
$shipping_amount = $order->getShippingAmount();
$order->setShippingAmount( $shipping_amount + $MyPaymentMethod->getPostHandlingCost() );
}
}
There may be a few other ways to do it, but this is my way of doing it and wanted to share with other Magento developer. If you know other way to do it please write it below.

Cristian, I tried to add events in \app\code\core\Mage\Paypal\etc\config.xml and create Observer.php in \app\code\core\Mage\Paypal\Module to update extra fee when check out. But it doesn’t work. Can you provide me sample source files? Thanks!
I can not provide you any sample source files, just try to tell me here what is your problem.
Below is what I did.
1./etc/config.xml
singleton
/observer
updateShippingAmount
2./Model/Observer.php
class mycommunity_mymodule_Model_Observer
{
public function updateShippingAmount($observer)
{
$order = $observer->getEvent()->getOrder();
$shipping_amount = $order->getShippingAmount();
$order->setShippingAmount($shipping_amount *(1+0.031)); //extra cost
return $this;
}
}
Questions. I tested event checkout_type_onepage_save_order . It seems it only works when I click “Place Order” but not after clicking payment. And the method setShippingAmount dosen’t work. I can’t figure out what is the problem since there is no developer cook book of Magento.
Yes, the event that you are observed is called when the order is placed. If you want to update the checkout summary page, you have to rewrite the views.
I see. Thanks! Do you know if the magento has event when payment is checked? I got these events from here.
http://activecodeline.com/wp-content/uploads/2009/03/magento-events-cheat-sheet.pdf
Not sure what where is called but can follow the code and see it. This is the way I do. You might check the “sales_convert_quote_to_order”.
Great idea. I am searching for a payment option that will allow me to add a handling fee to the order.
In my opinion it is better that the customer sees the fee when the payment option is selected, or at the order review page before the final placement of the order.
I see that you are modifying core files, maybe it is better to create a module?
I am not modifying the core files. What I post as example is part from a module that observer an event and do some actions.
I have done the same thing that you suggested, let the users to see the fee on the review page.
Hello,
Maybe you can help me. I am search for a way to add a handling fee for every product sku thats in the shopping cart.
Example.
3 sku23234 = handling fee €1,-
1 sku2323 = handling fee €1,-
100 sku45756 = handling fee €1,-
total handling fee €3,-
How can i make this work?
Cheers,
Jord
I want to migrate my site to Magento but I need to be able to accept a downpayment via paypal so the remaining balance can be paid the day of the tour at the facility
I do not know how to write to the file config.xml, Can you provide me sample source files? Thanks!
Have a look here on how to create a Magento config with event observer: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
I can’t get this to work. It seems to fail at $order->setShippingAmount( $shipping_amount + $MyPaymentMethod->getPostHandlingCost() );.
Is this way still possible with 1.4.1.1? Thanks for the head start though.