• SumoMe

EDIT Nov 5, 2011: This mod was tested on version 1.5.1.2

Last time I made a tutorial about adding custom fields in OpenCart 1.5, particularly in creating a new account. Today I will do the same using vQmod. But before I get to the tutorial, what is vQmod? According to its website,

VirtualQMod, or “vQmod” for short, is a new innovation in php modification override methods. Think of it as a universal “hook” that can be made to work with any project. The author goes by “Qphoria” (hence the Q), and is a co-developer of the OpenCart Ecommerce System

Basically, it is a safer alternative for modifying existing code (or “core files,” if I may), so that none of it is overwritten. In my opinion the advantage of using vQmod lies primarily in updating the original code while retaining one’s modifications, since the changes made are “virtual” and isolated beautifully.

The following tutorial requires you to be familiar of vQmod, so I suggest that you do some reading in the vQmod website. Have a look in the very intuitive Hello World example provided and surely you will get you right to it in no time.

Now for the tutorial, let’s get started:

Step 1: Add column to the database

Open your favorite MySQL database management tool or phpMyAdmin and look for the customer table. Add a new column for the salutation field, or you can also execute the following SQL script:

ALTER TABLE `customer` ADD `salutation` VARCHAR( 4 )
CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ;

Step 2: Install vQmod to OpenCart 1.5

Once you download vQmod, extract the vqmod folder in the package to the root folder of your OpenCart installation.

  • Install it by navigating to http://yourstore.com/vqmod/install in your web browser.
  • It will prompt you that the installation is successful afterwards.
  • Remember not to delete the install folder.

Step 3: Impose modifications in xml file.

Create an xml file in vqmod/xml folder and name it salutation_field_mod.xml (or whatever relevant file name you please). Open it and paste the following code:

<modification>
	<id>Salutation Field Modification</id>
	<version>1</version>
	<vqmver>1.0.8</vqmver>
	<author>maca</author>
	<file name="catalog/view/theme/default/template/account/register.tpl">
		<operation>
			<search position="before"><![CDATA[
				<td><span class="required">*</span> <?php echo $entry_firstname; ?></td>
			]]></search>
			<add><![CDATA[
				<td><?php echo $entry_salutation; ?></td>
				<td><input type="text" name="salutation" value="<?php echo $salutation; ?>" /></td>
				</tr>
				<tr>
			]]></add>
		</operation>
	</file>
	<file name="catalog/language/english/account/register.php">
		<operation>
			<search position="before"><![CDATA[
				$_['entry_firstname']      = 'First Name:';
			]]></search>
			<add><![CDATA[
				$_['entry_salutation'] = 'Salutation:';
			]]></add>
		</operation>
	</file>
	<file name="catalog/controller/account/register.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'];
				} else {
					$this->data['salutation'] = '';
				}
			]]></add>
		</operation>
	</file>
	<file name="catalog/model/account/customer.php">
		<operation>
			<search position="replace"><![CDATA[
				$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', 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']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW()");
			]]></search>
			<add><![CDATA[
				$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', 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']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW(), salutation = '" . $this->db->escape($data['salutation']) . "'");
			]]></add>
		</operation>
	</file>
</modification>

To reconcile this method to the last post,

Save the file and the modifications should work. Please notify me in the comments section if you notice some errors in the code above.

Click here for Part 2:

Adding Custom Fields in OpenCart 1.5 Using vQmod: Administration Panel