Breaking Magento code: Underscores and shipping methods

Code breaks – that’s just the nature of programming. And so identifying and fixing broken code becomes and integral part of the process.

To lend our hand in helping the world of coding we want to illustrate a seemingly small, but potentially very frustrating error that you can make when dealing with shipping methods in Magento.

The reason your Magento code can break

I want to share some interesting limitation in Magento. Namely — when creating new shipping method don’t use underscores in carrier or method code. Underscore in code has potential to break any code that uses this method — Mage_Sales_Model_Order::getShippingMethod. As an example I found out myself — I was missing shipping carrier in exported orders for certain shipping methods. Not cool at all if this happens live.

The reason lies here in Mage_Sales_Model_Order::getShippingMethod

Notice the explode function call. It takes string that looks something like ‘carriercode_methodcode’ and splits into parts by underscore. If however there are some additional underscores like in ‘carrier_code_method_code’ it will get wrong codes and for example getShippingCarrier() will fail to return carrier that was used on order.

So this is one more thing to remember in the vast sea of things to remember that Magento offers. Code safe!

Having trouble with Magento? You’ve come to the right place! Scandiweb is the most certified Magento agency in the world with more than a hundred experienced coders and more than a decade of experience under its belt. We’re here to help, so if you have any questions – drop us a line at [email protected] or check out our services page.

Related articles:

If you enjoyed this post, you may also like