Custom Validations for Product Options In Opencart

Opencart is very popular e-commerce platform. Each and every platform has their own pre-defined set of rules and functionalities. Such functionalities are useful to fulfil the requirements of any online ecommerce store. But, In real scenario these functionalities are not enough to fulfil all your requirements.
Sometimes you might have custom requirements that are not bundled as a part of default source. In such case custom development is the only option. To know about Custom Module Development read this:
In case of Opencart there are mainly two ways to modify the source to make it compatible with your requirements:
- Write vQmod module which don't override the core files. But this option requires extra knowledge of the core functionalities and how vQmod works.
- Write the custom code in the default source by maintaining the base flow. You just need to do as directed in the modification guide.
By default Opencart has only one validation rule of "required" field but not custom validation for the product options. In this tutorial we're going to learn How to Add the Validation for the Product Options in Opencart.
Note : This tutorial is tested on the version Opencart 2.0.3.1 and it's working fine. This might work(with no or some changes) in other lower versions also. Its strongly recommended that you take a backup of the Database and Source code before making changes explained in this post. Please keep the note of the changes file wise or bookmark this page :) so It will be useful in case of future up gradation.
The main purpose of this tutorial is to show you that how you can add custom validation rules for the product options in Opencart. In this tutorial we will create two simple validation rules for the text fields. You can make any rules for any fields by making few changes similar to the text fields.
Rule 1 : Restrict the numbers of characters
Rule 2 : Match the entered value with your pattern (regex)
This tutorial is divided in two parts based on the changes:
- Admin section changes
- Front end changes
In this post we will see the changes required in the admin section to add the custom validation rules for the product options.
Step:1
First of all we need to make one small change in the database to add new field in product_option
table.
Note : here oc_ is the database prefix, you can change it with your prefix.
ALTER TABLE `oc_product_option` ADD `validationrule` TINYINT( 1 ) NOT NULL DEFAULT '0'
Step:2
Open /admin/controller/catalog/product.php
file and search for the below line in the getForm()
function:
$data['text_yes'] = $this->language->get('text_yes');
and add the following line just after that:
$data['text_13max'] = $this->language->get('text_13max');
Now, in the same file search for the below line in the getForm()
function:
$data['entry_required'] = $this->language->get('entry_required');
and add the following lines just after that:
$data['entry_validationrule'] = $this->language->get('entry_validationrule');
Again in the same file, search for the below array (approx on line no. 1138):
$data['product_options'][]
in this array, add the below line at last:
'validationrule' => $product_option['validationrule']
after this it will looks like (note the "," added after required option value):
'required' => $product_option['required'],
'validationrule' => $product_option['validationrule']
Step:3
Open /admin/language/english/catalog/product.php
file and search for the below line:
$_['text_amount'] = 'Fixed Amount';
and add the below line just after that:
$_['text_13max'] = 'Maximum 13 Characters are allowed!';
Now, in the same file search for the next line:
$_['entry_required'] = 'Required';
and add the below line just after that:
$_['entry_validationrule'] = 'Validation Rule';
Step:4
Changes given in this steps needs to be placed carefully as it involves the main INSERT
and EDIT
queries. So make sure that you are making change at the correct place. Open /admin/model/catalog/product.php
file and add
validationrule = '" . (int)$product_option['validationrule'] . "'
in the else condition inside the below condition:
if (isset($data['product_option'])) {}
in addProduct($data)
function(approx line no:47).
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', value = '" . $this->db->escape($product_option['value']) . "', required = '" . (int)$product_option['required'] . "', validationrule = '" . (int)$product_option['validationrule'] . "' ");
Now in the same file add
validationrule = '" . (int)$product_option['validationrule'] . "'
in the else condition inside the below condition:
if (isset($data['product_option'])) {}
in editProduct($product_id, $data)
function(approx line no:179)
$this->db->query("INSERT INTO " . DB_PREFIX . "product_option SET product_option_id = '" . (int)$product_option['product_option_id'] . "', product_id = '" . (int)$product_id . "', option_id = '" . (int)$product_option['option_id'] . "', value = '" . $this->db->escape($product_option['value']) . "', required = '" . (int)$product_option['required'] . "', validationrule = '" . (int)$product_option['validationrule'] . "'");
Again in the same file,search for:
getProductOptions($product_id) function (approx line no:481)
inside this function, go to $product_option_data[]
array. in this array, add the below line at last:
'validationrule' => $product_option['validationrule']
after this it will looks like (note the "," added after required option value):
'required' => $product_option['required'],
'validationrule' => $product_option['validationrule']
Step:5
Open /admin/view/template/catalog/product_form.tpl
file. In this file we need to add the extra field for the custom validation rule. In this file search for the below code (approx line no:495):
<?php if ($product_option['type'] == 'text') { ?>
add the given block of code just before it.
<div class="form-group">
<label class="col-sm-2 control-label" for="input-validationrule<?php echo $option_row; ?>"><?php echo $entry_validationrule; ?></label>
<div class="col-sm-10">
<select name="product_option[<?php echo $option_row; ?>][validationrule]" id="input-validationrule<?php echo $option_row; ?>" class="form-control">
<?php if ($product_option['validationrule']=='13') { ?>
<option value="13" selected="selected"><?php echo $text_13max; ?></option>
<option value="0"><?php echo $text_no; ?></option>
<?php } else { ?>
<option value="13"><?php echo $text_13max; ?></option>
<option value="0" selected="selected"><?php echo $text_no; ?></option>
<?php } ?>
</select>
</div>
</div>
Now in the same file, search for the below code(approx line no:1170):
if (item['type'] == 'text') {
in this condition, add the below code just after it.
html += ' <div class="form-group">';
html += ' <label class="col-sm-2 control-label" for="input-required' + option_row + '"><?php echo $entry_validationrule; ?></label>';
html += ' <div class="col-sm-10"><select name="product_option[' + option_row + '][validationrule]" id="input-validationrule' + option_row + '" class="form-control">';
html += ' <option value="13"><?php echo $text_13max; ?></option>';
html += ' <option value="0" selected="selected"><?php echo $text_no; ?></option>';
html += ' </select></div>';
html += ' </div>';
That's it..!! You're done with the admin section. Now it's time to create new product and check that adding and editing product option rule working fine or not? Checkout the Next Part for the front end changes.