Quick Tip: Accept Multiple Vouchers in OpenCart 2.0

OpenCart is an open-source, PHP-based online e-commerce solution that allows you to accept orders online. It allows customers to shop using vouchers, but only one at a time by default. If you want to allow your customers to use multiple vouchers at once, you'll need to modify the code. I'll show you exactly how to do that in this tutorial.

How Do We Create a Gift Voucher?

Only admins and permitted back-end users are able to generate gift vouchers. OpenCart comes with a default module for doing it. The following screenshot shows the place where a back-end user can generate vouchers.

Dashboard

What Is the Problem?

If we have a voucher system implemented in OpenCart by default, what is the problem? Let's understand it with the help of an example:

Recently a friend of mine wanted to buy a couple of items through OpenCart that cost $800, but she only had $500, and two gift vouchers worth $250 each. So she called me and asked for a solution. Because OpenCart only allows you to use a single voucher to shop at any one time, she couldn’t buy the items.

A Solution

To resolve this problem, we need to add an option to use multiple vouchers so that customers can shop easily, without facing any difficulty.  Here is the solution, in which we will modify a few lines of code and it will help to solve this problem. 

This tutorial is divided into three main steps, which are:

  1. Change in Controller
  2. Change in Model
  3. Change in View

1. Controller File

  1. Navigate to catalog/controller/checkout/cart.php.
  2. Find the following code: 

Change the above code to:

2. Model File

  1. Navigate to catalog/model/total/voucher.php.
  2. Find the following lines of code:

We need to run an outer loop to fetch all of our vouchers that we maintained as array in Step 1. So we will place an outer loop after $this->load->model('checkout/voucher');.

So we will be looping our Session Vouchers Array to fetch all the vouchers we applied, and the code will be as shown below. Note that the following code is commented in order to make it easier to understand.

3. View File

  1. Navigate to catalog/view/theme/default/template/checkout/voucher.tpl.
  2. Find the following line of code:

Replace it with this:

We are done with our problem! In fact, we just made some simple code hacks to solve that big problem, so we didn't have to develop a new module or extension. We just modified some lines of codes to get it done.

Conclusion

In this article we provided a successful solution for adding multiple vouchers in our e-shop. Since OpenCart doesn’t allow shoppers to use multiple vouchers by default, we modified the code so that now they can use as many vouchers as they want. That will help customers to shop easily without any problem. 

In our next articles we will be implementing some real-world business tools in our OpenCart system, so stay subscribed and contribute your valuable comments. Thanks!

Tags:

Comments

Related Articles