How to Add Order Number to Order Confirmation Page in OpenCart 2.x


If you have used OpenCart you will probably be aware that, while being a very solid and streamlined ecommerce solution, it’s not without its quirks. One such quirk is the order confirmation page, which doesn’t actually show any details of the order, and instead directs customers to their account page. This is generally fine, but it doesn’t help guest users, and if a customer has accidentally input the wrong email address on the checkout they may find they are unable to retrieve their order number at all. Luckily, there is a simple fix to resolve this.

In OpenCart 2.x there are two files you need to edit – one is the page template used for order confirmations, and the other is the PHP controller that supplies data to this. We’ll look at the PHP controller file first, as this is where we compile the data we require to be displayed on the template. You’ll need to use an FTP client to navigate to the following file:

/catalog/controller/checkout/success.php

At the top of this file you will see the following line:


if (isset($this->session->data['order_id'])) {
$this->cart->clear();

The $this->cart->clear(); line actually clears out the session data, so you need to insert a query before this in order to set the data. The bit you’ll be inserting looks like this:


$this->load->model('checkout/order');
$data['orderDetails'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);

So your final code should look like this


if (isset($this->session->data['order_id'])) {
$this->load->model('checkout/order');
$data['orderDetails'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);
$this->cart->clear();

Remember to take a backup of any files before you overwrite them on the server.

Once you have made the above modification, you need to navigate to the template file, which can be found here:

/catalog/view/theme/YOUR-THEME-NAME/template/common/success.tpl

You need to replace YOUR-THEME-NAME with whatever directory your theme is in, so if you are using the default theme then your file will be found at /catalog/view/theme/default/template/common/success.tpl.

Once you have this file, you need to find the section where your content is displayed. It probably looks something like this:



<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>

You’ll know you’re in the right place if you seeĀ <?php echo $text_message; ?> just underneath. You will need to insert the following code somewhere between the two:


<?php if(!empty($orderDetails['order_id'])): ?>
<h2>Order Number: <?php echo $orderDetails['order_id']; ?></h2>
<?php endif; ?>

Your code should therefore look something like this:



<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>
<?php if(!empty($orderDetails['order_id'])): ?>
<h2>Order Number: <?php echo $orderDetails['order_id']; ?></h2>
<?php endif; ?>
<?php echo $text_message; ?>

As always, take a backup first, and then upload your modified file to the server. Once you’re done, you’ll need to clear your modifications cache for the changes to take effect. Log in to your OpenCart installation and navigate to Extensions -> Modifications and click the blue ‘refresh’ icon in the top right – it won’t work until you’ve done this.

You can test the changes by placing a test order, where you will hopefully see the new order number message shown above the other messages in the content area. It’s worth bearing in mind that this is based on the session data from your transaction, so manually loading the order confirmation page (usually https://www.mywebsite.com/checkout-success) won’t allow you to see an order number, you’ll have to actually place an order in order each time you want to test it out.

We’ve tested the above on OpenCart versions 2.3.0.2 and 2.2.0.0 but it should work on all v2.x versions of OpenCart. Remember to always test on a development server before rolling out on a live environment!