WELCOME! Thanks for visiting the Catzie.net Blog. Find out More

OpenCart: Override Invoice ID

Edit: Seems like there’s something missing in the code I inserted. I only found out after further system testing. Will update this post when I feel like fixing it. :p

I’m taking an E-Commerce class (it’s my last school term!) and my group’s project in an online store that uses OpenCart, an open-source shopping cart software. Our professor wants us to have transaction numbers (invoice ID, in OpenCart) that are not simply the increment of the previous one (0001, 0002, 0003, …).

It took me long to figure out how to edit the invoice ID that OpenCart generates. In the settings page of the admin area we can only edit the prefix (default is INV) and the starting invoice ID (default is 001). When I change either of the two fields the invoice ID that is generated doesn’t change at all. After some research I found out that the function which generates the invoice ID is set to get the highest ID value in the database and increment it by 1. That’s how it generates new invoice ID’s.

Below is the function that generates the invoice ID (found at admin/model/sale/order.php):

public function generateInvoiceId($order_id) {
$query = $this->db->query("SELECT MAX(invoice_id) AS invoice_id FROM `" . DB_PREFIX . "order`");

if ($query->row['invoice_id']) {
$invoice_id = (int)$query->row['invoice_id'] + 1;
} elseif ($this->config->get('config_invoice_id')) {
$invoice_id = $this->config->get('config_invoice_id');
} else {
$invoice_id = 1;

$this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_id = '" . (int)$invoice_id . "', invoice_prefix = '" . $this->db->escape($this->config->get('config_invoice_prefix')) . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'");

return $this->config->get('config_invoice_prefix') . $invoice_id;

What I did was comment out everything inside the function and insert the statement:

return date("Ymd-") . rand(0,9999);

This results to an invoice ID that contains current date, a hyphen, and a 4-digit number. For example: 20110313-1234.


Add a Comment

Your email address will not be published. Required fields are marked *