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.

7 thoughts on “OpenCart: Override Invoice ID”

  1. Cheers, very nice! Thanks for posting. But what would you do if you want the order number to be the invoice number as well? Please advise! Thank you. Alex

  2. Hi Catzie, nice blog site! I was searching for some topics on CMS when suddenly I ended up here on your blog. Do you know how to link Opencart in Joomla or WordPress? Thanks. Though I’ll keep searching, I’ll appreciate if you could give me some help. Thank you so much again and keep blogging :-)

  3. Its true the comment above about using RAND is correct this method needs and if statement that will stop the possibility of a duplicate other than that this helped put is on the right path! So thank you for the walk through above!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>