How to Automatically Generate Invoice Numbers in OpenCart 2.x
Generating order invoices is a major part of the management of an online store. Luckily, OpenCart enables you, as a store admin, to generate order invoices with just a few clicks.
However, as you may be aware of, order invoice numbers are not automatically added to the invoice, but they have to be pre-generated. Currently, this is done in the following way:
As you can see, this process is rather cumbersome and in this tutorial we will demonstrate how you can automate it. So keep on reading if you would like to know how you can save time by adding a simple modification, which will automatically generate invoice number at the moment of order placing.
Step 1:
Start off by opening your favorite text editor (DreamWeaver, Sublime, Notepad, etc.) and pasting the following code:
<modification> <name>Generate invoice number automatically</name> <version>1.0</version> <link>https://isenselabs.com</link> <author>iSenseLabs</author> <code>isenselabs_generate_invoice_number</code> <file path="catalog/model/checkout/order.php"> <operation> <search><![CDATA[if ($order_info) {]]></search> <add position="after"><![CDATA[ if (empty($order_info['invoice_no']) || $order_info['invoice_no'] == 0) { $query = $this->db->query("SELECT MAX(invoice_no) AS invoice_no FROM `" . DB_PREFIX . "order` WHERE invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "'"); if ($query->row['invoice_no']) { $invoice_no = (int)$query->row['invoice_no'] + 1; } else { $invoice_no = 1; } } else { $invoice_no = $order_info['invoice_no']; } ]]></add> </operation> <operation> <search><![CDATA[$this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'");]]></search> <add position="replace"><![CDATA[ $this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_no = '" . (int)$invoice_no . "', invoice_prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "', order_status_id = '" . (int)$order_status_id . "', date_modified = NOW() WHERE order_id = '" . (int)$order_id . "'"); ]]></add> </operation> <operation> <search><![CDATA[$data['text_order_detail'] = $language->get('text_new_order_detail');]]></search> <add position="after"><![CDATA[ $data['text_invoice_no'] = $language->get('text_new_invoice_no'); ]]></add> </operation> <operation> <search><![CDATA[$data['order_id'] = $order_id;]]></search> <add position="before"><![CDATA[ $data['invoice_no'] = $invoice_no; ]]></add> </operation> </file> </modification>
Step 2:
Save the file with the extension .ocmod.xml (e.g. invoice_num_generation.ocmod.xml) and upload it through the extension installer (Admin Panel > Extensions > Extension Installer).
Step 3:
Refresh the modifications and test the feature by first making a test order and then going to your Admin Panel > Sale > Orders > You test order. Select the order and click on Print Invoice. If the modifications have been applied correctly, you should get the following result without having the need to pre-generate invoice number:
*Please, note that this modification will work only for orders placed after the modification has been applied.
We hope that this tutorial will help you save time and effort in the order invoice generation process. Any questions and suggestions are welcome in the comments section bellow.