Blog navigation keyboard_arrow_down

Blog Rss rss_feed

How to Create a Shipping Module in PrestaShop: A Complete Guide with Code and Structure Explanation

How to Create a Shipping Module in PrestaShop: A Complete Guide with Code and Structure Explanation

PrestaShop is one of the most popular platforms for building online stores, and customizing the shipping process is essential for improving both customer experience and business efficiency. In this guide, we’ll explore how to create a custom shipping module, explain key methods, demonstrate code examples, and highlight the importance of a well-structured design approach.

Why Develop a Custom Shipping Module in PrestaShop?

A custom shipping module enables the automatic calculation of shipping costs based on conditions specific to your business: weight, order value, region, or other criteria. This enhances the user experience and streamlines business operations, saving time and reducing errors.

Benefits of a Custom Shipping Module:

  • Flexibility: Configure unique shipping rules and integrate external logistics services.
  • Time Efficiency: Automates calculations, eliminating manual processes.
  • Transparency: Customers receive accurate and clear shipping cost estimates.

Core Principles for Creating a Shipping Module in PrestaShop

Creating a shipping module involves extending the CarrierModule class, a specialized PrestaShop class designed to handle shipping-related functionalities.

1. Extending the CarrierModule Class

Extending the CarrierModule class instead of the standard Module class ensures smooth integration with PrestaShop’s shipping management system. This gives your module direct access to shipping-related APIs and methods.

Step-by-Step Guide to Creating a Shipping Module

Step 1: Generate the Module Framework

To get started, you can use the PrestaShop Module Generator for creating the basic structure of your module. Specify the name, description, and other essential parameters.

Step 2: Writing the Module Code

Below is a complete example of a functional shipping module:

<?php if (!defined('_PS_VERSION_')) { exit; } class MyOwnCarrier extends CarrierModule { public function __construct() { $this->name = 'myowncarrier'; $this->tab = 'shipping_logistics'; $this->version = '1.0.0'; $this->author = 'Your Name'; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('My Custom Carrier'); $this->description = $this->l('A module for calculating shipping costs.'); } public function install() { return parent::install() && $this->registerHook('actionCarrierUpdate') && $this->createCarrier(); } public function uninstall() { $carrier = new Carrier((int) Configuration::get('MYCARRIER_CARRIER_ID')); if (Validate::isLoadedObject($carrier)) { $carrier->delete(); } return parent::uninstall(); } private function createCarrier() { $carrier = new Carrier(); $carrier->name = 'My Carrier'; $carrier->is_module = true; $carrier->active = true; $carrier->deleted = 0; $carrier->delay = array( 'en' => 'Fast delivery', 'fr' => 'Livraison rapide', 'ru' => 'Быстрая доставка', ); $carrier->shipping_handling = false; $carrier->range_behavior = 0; $carrier->is_free = false; $carrier->shipping_external = true; $carrier->external_module_name = $this->name; $carrier->need_range = true; if ($carrier->add()) { Configuration::updateValue('MYCARRIER_CARRIER_ID', (int)$carrier->id); $this->addCarrierRanges($carrier); $zones = Zone::getZones(true); foreach ($zones as $zone) { $carrier->addZone((int)$zone['id_zone']); } return true; } return false; } private function addCarrierRanges(Carrier $carrier) { $rangePrice = new RangePrice(); $rangePrice->id_carrier = (int) $carrier->id; $rangePrice->delimiter1 = 0; $rangePrice->delimiter2 = 1000; $rangePrice->add(); $rangeWeight = new RangeWeight(); $rangeWeight->id_carrier = (int) $carrier->id; $rangeWeight->delimiter1 = 0; $rangeWeight->delimiter2 = 10; $rangeWeight->add(); } public function hookActionCarrierUpdate($params) { $id_carrier_old = (int) $params['id_carrier']; $id_carrier_new = (int) $params['carrier']->id; if ($id_carrier_old === (int) Configuration::get('MYCARRIER_CARRIER_ID')) { Configuration::updateValue('MYCARRIER_CARRIER_ID', $id_carrier_new); } } public function getOrderShippingCost($params, $shipping_cost) { if ($this->id_carrier === (int) Configuration::get('MYCARRIER_CARRIER_ID')) { return (float) ($shipping_cost + 10); // Adds a fixed surcharge } return false; // If the carrier is inactive } }

Why Is Proper Module Design Important?

1. Methods for Calculating Shipping Costs

  • getOrderShippingCost() — used for calculating shipping costs based on predefined ranges of weight or price.
  • getOrderShippingCostExternal() — allows integration with external logistics services or APIs, providing dynamic rate adjustments.

2. Hook hookActionCarrierUpdate()

This hook ensures that changes to the carrier ID during backend editing do not disrupt the module’s functionality, maintaining stability and reliability.

Creating a shipping module in PrestaShop is a crucial step towards optimizing your store’s logistics. By following the best practices and using standard methods, you ensure that your solution remains flexible, reliable, and easy to maintain. This approach not only enhances the user experience but also simplifies logistics management, opening up new opportunities for business growth.

Feel free to share your questions and ideas in the comments!

Was this blog post helpful to you?

    
No comments at this moment
close

Checkout

close

Favourites