Carrier modules - functions, creation and configuration
This article was written by Fabien Serny, and published on September 28th, 2011 on the PrestaShop blog.
Foreword
Please note, this article is aimed at advanced developers (knowledge of hooks, the Carrier object...) and applies to PrestaShop 1.4 and later. To find out what a hook is, I recommend reading the article by Julien Breux here: Understanding and using hooks
On a basic level PrestaShop lets you create carriers and configure how shipping fees are calculated based on weight and price ranges that you can specify in the back office.
The limits of this system are reached when you want to retrieve prices via webservices (UPS, USPS, Fedex) or want to make your own system for calculating shipping fees (by number of items in the basket, restricting yourself to one carrier or one or more countries, etc.).
This is when carrier modules come into play.
Where do I start?
There is a basic module you can download here. The article will start from this basic module but of course you can modify it and adapt it to your needs
In our example, you can manage two carriers and set an additional cost for them in addition to the price by range configured in the back office.
Certain parts of the code are deliberately duplicated to help you understand the mechanism. It is up to you to manage your specific list of carriers (using a data table for example).
Explanation of the example
First, please note the fact that the module belongs to the CarrierModule class and not the Module class. For now, let’s make a short list of the methods contained in this module and the way it works in general.
1) The __construct, install and uninstall methods
Construct:
Install:
Uninstall:
2) Back office methods
I will not dwell on most of the back office methods (getContent, _displayForm, _postValidation and _postProcess), which are relatively simple and are there to enable you to set up carrier costs. I will let you discover them in the example module.
However, I would like to draw your attention to the hookupdateCarrier method below. In PrestaShop, every time you edit a carrier, the carrier is automatically archived and a new carrier is created.
Technically, PrestaShop changes the deleted flag of the carrier to 1 and create a new one. This is why when you edit a carrier via the "Carriers" tab in your back office, its id changes.
You therefore have to hook the module in order to update the carrier id when it changes.
3) Front office methods
Taking it further
The example here is very simple, but you can create more complex modules by associating them with webservices for example (as is the case with the UPS modules), or perform a calculation based on the number of products in your cart.
In short, you’ve now got the hang of calculating shipping fees
Last updated
Was this helpful?
