• Sumo

In a previous post, I shared a vQmod XML file that enables the customer in OpenCart (version 1.5) to add an additional field when creating a new account with the Salutation field. However, what if you want to view and edit the Salutation field in the administration panel? In this post I will share to you another vQmod file that enables you, the store administrator, to include your newly added customer field when viewing, adding, and editing each customer in the administration panel.

Before I begin, allow me to emphasize that if you plan on modifying core files of OpenCart, using vQmod is the best option because it isolates all the modifications you make. This way you can preserve the changes whenever you update your OpenCart installation. You can learn more about vQmod by visiting its website. The learning curve is as flat as it can be, IMHO.

Okay, let’s get started:

Create a new XML file in vqmod/xml from your root folder and paste the following code. If you want, you may also append the code from Line 6 to Line 70 after the last block of <file> tag in your previous XML file. This is under the assumption that you already accomplished the one in the previous post.

<modification>
    <id>Salutation Field Modification - Admin Panel</id>
    <version>1</version>
    <vqmver>1.0.8</vqmver>
    <author>maca</author>
    <file name="admin/view/template/sale/customer_form.tpl">
        <operation>
            <search position="before" offset="2"><![CDATA[
                <td><input type="text" name="firstname" value="<?php echo $firstname; ?>" />
            ]]></search>
            <add><![CDATA[
                <tr>
                    <td><?php echo $entry_salutation; ?></td>
                    <td><input type="text" name="salutation" value="<?php echo $salutation; ?>" /></td>
                </tr>
            ]]></add>
        </operation>
    </file>
    <file name="admin/language/english/sale/customer.php">
        <operation>
            <search position="before"><![CDATA[
                $_['entry_firstname']       = 'First Name:';
            ]]></search>
            <add><![CDATA[
                $_['entry_salutation']  = 'Salutation:';
            ]]></add>
        </operation>
    </file>
    <file name="admin/controller/sale/customer.php">
        <operation>
            <search position="before"><![CDATA[
                $this->data['entry_firstname'] = $this->language->get('entry_firstname');
            ]]></search>
            <add><![CDATA[
                $this->data['entry_salutation'] = $this->language->get('entry_salutation');
            ]]></add>
        </operation>
        <operation>
            <search position="before"><![CDATA[
                if (isset($this->request->post['firstname'])) {
            ]]></search>
            <add><![CDATA[
                if (isset($this->request->post['salutation'])) {
                    $this->data['salutation'] = $this->request->post['salutation'];
                } elseif (isset($customer_info)) {
                    $this->data['salutation'] = $customer_info['salutation'];
                } else {
                    $this->data['salutation'] = '';
                }
            ]]></add>
        </operation>
    </file>
	<file name="admin/model/sale/customer.php">
		<operation>
			<search position="replace"><![CDATA[
				$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', newsletter = '" . (int)$data['newsletter'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', password = '" . $this->db->escape(md5($data['password'])) . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
			]]></search>
			<add><![CDATA[
				$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', newsletter = '" . (int)$data['newsletter'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', password = '" . $this->db->escape(md5($data['password'])) . "', status = '" . (int)$data['status'] . "', date_added = NOW(), salutation = '" . $this->db->escape($data['salutation']) . "'");
			]]></add>
		</operation>
		<operation>
			<search position="replace"><![CDATA[
				$this->db->query("UPDATE " . DB_PREFIX . "customer SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', newsletter = '" . (int)$data['newsletter'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', status = '" . (int)$data['status'] . "' WHERE customer_id = '" . (int)$customer_id . "'");
			]]></search>
			<add><![CDATA[
				$this->db->query("UPDATE " . DB_PREFIX . "customer SET firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', newsletter = '" . (int)$data['newsletter'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', status = '" . (int)$data['status'] . "', salutation = '" . $this->db->escape($data['salutation']) . "' WHERE customer_id = '" . (int)$customer_id . "'");
			]]></add>
		</operation>
	</file>
</modification>

Explaining the code:

Knowing that OpenCart is using the MVC-L (Model View Controller – Language) code structure, all four files that correspond to each component is modified. The first <file> block modifies the view file (This page is located when you navigate to Sales>Customers>Customers in the administration panel and press Edit on one of the customer records). The second <file> block modifies the language file. The third <file> block modifies the controller, which glues everything together. Lastly, the fourth <file> block modifies the model file so that the changes made can be reflected to the database.

Here is what it will look like afterwards:

The newly created field, Salutation is shown in the edit and insert page of customers in the OpenCart administration panel (before the First Name). Salutation is not required and the value entered is saved accordingly.

I hope this tutorial made some sense. Please let me know what you think.