World's THINNEST Laptop »

How To Create or Clone a New Shipping Module in ZenCart

This is a continuation of a previous tutorial where we had learned how to customize the text of the "Regular flat rate" shipping module. Now in this tutorial, we will create a new shipping module that is to be called "Priority Flat Rate Shipping" module that is based on the regular "Flat Rate" shipping module. In other words, we are going to clone the Flat Rate shipping module and call it Priority Shipping Module so that the customer can pick between the two shipping methods (which has different flat rate prices).

First we duplicate the regular flat rate shipping module file "includes/modules/shipping/flat.php" and call it flatpriority.php in the same folder.

We also duplicate the regular flat rate language file includes/languages/english/modules/shipping/flat.php and call it flatpriority.php in the same folder.

In the new language file includes/languages/english/modules/shipping/flatpriority.php, we give it a new set of constants with the new text values as shown...

And in the new module file includes/modules/shipping/flatpriority.php, we replaced the existing constants with the new constants ...

As shown above, we also changed the class name and class constructor from flat to flatpriority. And we also change the $this->code to have the value flatpriority instead of flat. It is important to have this value be the same as the filename.

New Database Configuration Keys

Our new priority flat rate shipping module will need new configuration keys for use in the ZenCart configuration database table. We can not use the existing keys that was used for the regular flat rate shipping module.

So we change the six configuration keys in the keys() function in module file includes/modules/shipping/flatpriority.php ...

So for example, instead of the keys...

MODULE_SHIPPING_FLAT_STATUS,
MODULE_SHIPPING_FLAT_COST,
MODULE_SHIPPING_FLAT_TAX_CLASS,
MODULE_SHIPPING_FLAT_TAX_BASIS,
MODULE_SHIPPING_FLAT_ZONE,
MODULE_SHIPPING_FLAT_SORT_ORDER,

we changed them to ...

MODULE_SHIPPING_FLATPRIORITY_STATUS,
MODULE_SHIPPING_FLATPRIORITY_COST,
MODULE_SHIPPING_FLATPRIORITY_TAX_CLASS,
MODULE_SHIPPING_FLATPRIORITY_TAX_BASIS,
MODULE_SHIPPING_FLATPRIORITY_ZONE,
MODULE_SHIPPING_FLATPRIORITY_SORT_ORDER

In the install function of the module file, we changed it to insert these new six keys into the database ...

As shown above, we also made two text changes to the admin configuration instructions text to say "priority flat rate shipping".

Now we have to look through the code of the module file to see if there are any more old keys that needs to be changed to the new keys. Eight more instances are found here ...

Note that this tutorial refers to ZenCart version 1.3.8. Your version may differ.

Download Example Files

Here are the two completed example files from these two tutorials: shipping_module_example.zip. Files are for learning purposes and we assume no liability nor warranties.

Test New Shipping Module

Now upload the two flatpriority.php files to server and install the flat priority shipping module. Test to make sure that you can save different separate values to the regular flat rate shipping as well to the priority flat rate shipping. Test to make sure you can turn on and off each of the module individually.

If all goes well, you can turn on both modules and customer should see ...

Trouble Shooting in New Shipping Module

If not go well, check to make sure you have done all the text replacements correctly. It is very easy to make mistakes. You may need to remove the shipping modules, delete the two flatpriority.php files from server, upload the two flatpriority.php files again, install the modules again.

Check whether the database configuration table has the correct records and correct configuration keys by this query...

SELECT * FROM `configuration` WHERE `configuration_key` like 'MODULE_SHIPPING_FLAT%'

You should have 12 records when both the regular and priority flat rate shipping modules are installed correctly (six for each module)

And you should have no records, if no shipping module is installed.

If you get warnings in your admin saying something like "Passed variable is not an array or object" or "Variable passed to each() is not an array or object", check to make sure that the value of "$this->code" in the shipping module file flatpriority.php matches the filename. If your filename is flatpriority.php, this value must be flatpriority.

If you turn on the new shipping module and the status dot is yellow instead of green, it does not necessarily mean a coding problem. It could be a configuration setting problem -- such as setting a shipping module to a non-existing zone or something.

The ZenCart forum thread linked here talks about cloning a shipping module and they also might be able to help.