Adding Custom Fields in OpenCart 1.5 Using vQmod: Administration Panel

  • SumoMe

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.

About maca

Hello, my name is Karlo Macariola. I once dreamed to be a painter or an animator but when I typed my first line of code I immediately fell in love with programming. Today my passions are with web development and web design though sometimes I still withdraw to my first love which is drawing. DeviantArt | Twitter | Facebook
This entry was posted in Web Development & Design and tagged , . Bookmark the permalink.

6 Responses to Adding Custom Fields in OpenCart 1.5 Using vQmod: Administration Panel

  1. Pingback: OpenCart: How do I add custom fields to my store pages | Maca is Rambling

  2. Pingback: Adding Custom Fields in OpenCart 1.5 Create Account, Using vQmod | Maca is Rambling

  3. meer says:

    so nice to see your work
    im working on the simple admin pannel customization ,, i wanna hide the help otion ai admin pannel ,and som others options also , bt till now i failled to do that

    if u wanna make it easy to me , i will b thankful to u
    meer

  4. Fabian says:

    My friend, your tutorial is amazing!!!! thanks, i’ve used it for including a mobile phone field, and playing a little bit with your original instructions, and messing around with the code, i’ve included that option in the edit account information form.

    I will try to include a mobile phone field in the delivery addres options.

    Thankyou so much for your help.

    • Chorng says:

      Hi Fabian,
      I’m a code dummie, can you share with me the xml file to add the phone field in the ‘delivery details’ on the check out page at ‘step 3’? your help is much appreciated !!

  5. phpmate says:

    hi,
    i want to one field in product -General section
    field name is — meta title this will come after product name
    i tries above method bt m still unable to get that
    so help me to get this ….
    thnks in advnce……plss reppp

Leave a Reply to meer Cancel reply

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