Create a Custom "Order Total" Extension in OpenCart

In this article, we’re going to create a custom order totals extension in OpenCart. The order totals extensions allow you to manipulate the order amount during the checkout. It’s a really useful way to alter the price by either adding extra taxes or providing discount through the different methods. We’ll create a fully fledged custom order totals extension which will apply a custom tax defined by the back-end configuration form.

To create a custom order totals extension, we need to set up files in the back-end and front-end. The back-end files are used to set up a configuration form, and the front-end files are used to define the logic of the extension. Of course, OpenCart won’t detect your extension during checkout without front-end files.

We’ll use the latest version of OpenCart. Also, I assume that you’re familiar with the basic module development process in OpenCart. If you are not familiar with it, here’s a nice article explaining custom module development.

Let’s go ahead and start right away!

Back-End File Setup

In this section, we’ll create files related to the back-end section. At the end of this section, you’ll be able to see our custom order totals extension in the list along with the other order totals extensions. Also, you’ll be able to install and configure it using the custom configuration form.

Go ahead and create a controller file admin/controller/total/customot.php with the following contents.

As you can see, it’s a fairly standard back-end controller set up in OpenCart. The main purpose of this controller file is to set up the labels and other elements which will be used to display the configuration form. Of course, it handles the form submission by validating a form and saving values to the database.

Now, let’s go ahead and create a language file at admin/language/english/total/customot.php with the following contents.

Again, It should be pretty easy to understand as we’re just defining labels.

Finally, we’ll create a view template file at admin/view/template/total/customot.tpl.

This file contains the XHTML code for our configuration form.

So that’s it as far as our back-end set up is concerned. Go to Extensions > Order Totals. You should be able to see our extension Custom Order Total listed along with other extensions. Let’s install and configure it as shown in the following screenshot.

Back-End Form

I’ve set the Custom Tax value to 5, so it’ll charge 5% of the total order value. Fill in the values and save the form.

Front-End File Setup

In this section, we’ll define files for the front-end, so that our extension is detected in the front-end checkout flow.

Create a model file catalog/model/total/customot.php with the following contents.

It’s an important file and the central logic of our extension goes here. Conventionally, OpenCart calls the getTotal method for every order totals extension during the checkout. You should notice the important arguments $total_data, $total and $taxes.

The $total_data variable represents an array of all the order total extensions data. The $total variable is the total order amount, and $taxes contains the taxes applied.

In that method, we’re fetching a Custom Tax value which was set from the back-end configuration form. Further, we calculate the custom tax amount and assign it to the $customtax_value variable. Next, we plug in our order totals extension information to the $total_data array and add the custom tax amount to the $total variable.

Finally, we need to define a language file at catalog/language/english/total/customot.php with the following contents.

So that’s it for the front-end!

Demo in the Front-End

Go ahead and add products to cart, and you’ll be able to see our custom tax applied as shown in the following screenshot.

Demo

So in this way, you could influence the order amount using the order totals extension.

Conclusion

In this article, we've learned how to create a custom order totals extension and manipulate the order amount. It’s a really useful way to attach any custom taxes and discounts. 

If you're looking for additional OpenCart tools, utilities, extensions, and so on that you can leverage in your own projects or for your own education, don't forget to see what we have available in the marketplace.

Suggestions and queries are always welcome!

Tags:

Comments

Related Articles