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.
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:
- Change in Controller
- Change in Model
- Change in View
1. Controller File
- Navigate to
catalog/controller/checkout/cart.php
. - Find the following code:
If(isset($this->request->post['voucher']) && $this-> valid voucher()) { $this->session->data['voucher']=$this->request->post['voucher']; }
Change the above code to:
If(isset($this->request->post['voucher']) && $this-> valid voucher()) { $this->session->data['voucher'][$this->request->post['voucher']]=$this->request->post['voucher']; // creates an array for multiple vouchers }
2. Model File
- Navigate to
catalog/model/total/voucher.php
. - Find the following lines of code:
$this->load->model('checkout/voucher'); $voucher_info = $this->model_checkout_voucher->getVoucher($this->session->data['voucher']); if ($voucher_info) { if ($voucher_info['amount'] > $total) { $amount = $total; } else { $amount = $voucher_info['amount']; } $total_data[] = array( 'code' => 'voucher', 'title' => sprintf($this->language->get('text_voucher'), $this->session- >data['voucher']), 'text' => $this->currency->format(-$amount), 'value' => -$amount, 'sort_order' => $this->config->get('voucher_sort_order') ); $total -= $amount; }
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.
foreach (array_unique($this->session->data['voucher']) as $voucher) { // foreach loop will select each array and extract the unique voucher $voucher_info = $this->model_checkout_voucher->getVoucher($voucher); // fetch the order details // Check 1: If Voucher Exists if ($voucher_info) { // Check 2: If the voucher amount is greater than our order amount, voucher balance will be maintained if ($voucher_info['amount'] > $total) { $amount = $total; } else { $amount = $voucher_info['amount']; } // End Check 2 // Array to return Updated Totals $total_data[] = array( 'code' => 'voucher', 'title' => sprintf($this->language-> get('text_voucher'), $voucher), 'text' => $this->currency->format(-$amount), 'value' => -$amount, 'sort_order' => $this->config->get('voucher_sort_order') ); // End Array $total -= $amount; // Substracts the amount with our order totals } // End Check 1 } // End Foreach Loop
3. View File
- Navigate to
catalog/view/theme/default/template/checkout/voucher.tpl
. - Find the following line of code:
<input type="text" name="voucher" value="<?php echo $voucher; ?>" placeholder="<?php echo $entry_voucher; ?>" id="input-voucher" class="form-control" />
Replace it with this:
<input type="text" name="voucher" value="" placeholder="<?php echo $entry_voucher; ?>" id="input-voucher" class="form-control" />
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!
Comments