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.
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
RAND makes it possible for there to be duplicate invoice numbers
thanks alot for posting and providing free sevices fo us.u may long live.
Can the same be done with OrderID#?
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 🙂
this has just saved my life!
thanks a lot
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!