Create a Custom Shipping Method in OpenCart: Part One

Although OpenCart provides a number of useful shipping methods in the core itself, there's always a chance that you'll need to create your own. On the other hand, being a web developer, you'll always try to explore the framework of your choice to see how to create your own custom stuff! 

In this series, we're going to create a custom shipping method module in OpenCart. It'll be a two-part series, and in the first part we'll create a back-end configuration form for our custom shipping method.

To create a new custom shipping method in OpenCart requires implementation of the files as per the conventions of OpenCart. In the back-end, you'll need to provide a configuration form which allows the administrator to configure the price, geo-zone and other parameters related to the shipping method. In the front-end, you'll implement the required files so that your custom shipping method is picked up during the checkout!

Today, we'll go through the back-end setup. I assume that you're using the latest version of the OpenCart. In the second part, we'll explore the front-end counterpart, in which we'll see the front-end file setup and demo in the front-end.

A Glance at the File Setup in the Back-End

Let's start with a list of the files required in the back-end. We'll use "custom" as the name of our custom shipping method.

  • admin/controller/shipping/custom.php: It's a controller file in which we'll set up everything necessary for the configuration form.
  • admin/language/english/shipping/custom.php: It's a language file in which we'll define the labels for our form.
  • admin/view/template/shipping/custom.tpl: It's a view template file which holds the HTML code of our configuration form.

So that's it as far as the back-end setup is concerned.

File Setup

Let's start with the controller setup.

Create a Controller File

Create a file admin/controller/shipping/custom.php and paste the following contents in that file.

It's an important file which defines most of the logic for the back-end configuration form. We'll go through the important snippets in the index method of the controller. As per the convention, you'll need to define the class name ControllerShippingCustom.

In the index method, we start by loading the language file and setting up the page title.

Next, we load the setting model and save the settings into the database available as a POST data of the configuration form. Before saving data, we validate the form using the validate method defined in that file.

After that, we assign language labels into the $data array so that we can access those in the view template file.

Moving ahead, there's a standard snippet to set up the proper breadcrumb links.

Next, we set up the action variable to make sure that the form is submitted to our index method. And in the same way, users are taken back to the list of shipping methods if they click on the Cancel button.

Further, there's code to populate the default values of the configuration form fields either in add or edit mode.

In the next section, we load the tax classes and geo zones from the database, which will be used as the drop-down options in the configuration form.

Finally, we assign the children templates and the main template of the view.

Create a Language File

Create a file admin/language/english/shipping/custom.php and paste the following contents in that file.

The contents of the file should be self-explanatory!

Create a View File

Create a file admin/view/template/shipping/custom.php and paste the following contents in that file.

Again, this should be pretty easy to understand. The purpose of this template file is to provide the configuration form for our custom shipping method. It uses the variables which we set up earlier in the controller file.

So, that's it as far as the back-end file setup is concerned for our custom shipping method. In the next section, we'll see how to enable our custom shipping method and what the custom configuration form looks like!

Enable the Custom Shipping Method

Head over to the admin section and Go to Extensions > Shipping. You should see that our custom shipping method is listed as Custom Rate. Click on the + sign to install our custom shipping method. After installing, you should be able to see the Edit link to open the configuration form. Click on the Edit link and the form should look as shown in the following screenshot.

Configuration Form

The important fields in the above form are Tax Class and Geo Zone.

The Tax Class field allows you to select the appropriate option if you need to impose any further tax in addition to the amount defined in the Cost field. Let's select Taxable Goods for now.

The Geo Zone field allows you to select the region to which this method is applicable; for the sake of simplicity select All Zones. Also, make sure to set the status to Enabled, otherwise it won't be listed in the front-end checkout.

Hit the Save button once you've filled in the necessary data and you should be fine. That's it for today's article, and I'll get back to you soon with the next part, which will explain the front-end file setup.

Conclusion

Today, we started a series on how to create a custom shipping method in OpenCart. In this first part, we went through the back-end section and explored how to set up the configuration form. Post your queries and suggestions if any!

Tags:

Comments

Related Articles