Search code examples
phpmysqlopencartopencart2.x

Opencart Custom Field not allowing Hyphen character "-"


I have successfully added a custom field(source_code) to my product by following this tutorial: http://www.php-dev-zone.com/2015/01/how-to-add-custom-product-field-in.html

However, my field will not allow the "-" character even though I have mimicked the "model" field which does allow hyphens.

For example if I enter "408000-025" the field enters "408000" into the database. If I enter "408000025" I receive the "408000025" in the database. If I enter the "408000-025" into the model field, I receive "408000-025".

Both the model and the source_code(my custom field) are varchar(64), utf8_general_ci fields in my database.

Below is my vqmod:

<file name="admin/model/catalog/product.php">
    <operation>
        <search position="replace"><![CDATA[
        $this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
        ]]></search>
        <add><![CDATA[
        $this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', source_code = '" . (int)$data['source_code'] . "', date_added = NOW()");
        ]]></add>
    </operation>
    <operation>
        <search position="replace"><![CDATA[
        $this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");
        ]]></search>
        <add><![CDATA[
        $this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', ean = '" . $this->db->escape($data['ean']) . "', jan = '" . $this->db->escape($data['jan']) . "', isbn = '" . $this->db->escape($data['isbn']) . "', mpn = '" . $this->db->escape($data['mpn']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . (int)$data['tax_class_id'] . "', sort_order = '" . (int)$data['sort_order'] . "', source_code = '" . (int)$data['source_code'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");
        ]]></add>
    </operation>
</file>
<file name="admin/controller/catalog/product.php">
    <operation>
        <search position="after"><![CDATA[
        $data['entry_sort_order'] = $this->language->get('entry_sort_order');
        ]]></search>
        <add><![CDATA[
        $data['entry_source_code'] = $this->language->get('entry_source_code');
        ]]></add>
    </operation>
    <operation>
        <search position="after"><![CDATA[
        $data['help_isbn'] = $this->language->get('help_isbn');
        ]]></search>
        <add><![CDATA[
        $data['help_source_code'] = $this->language->get('help_source_code');
        ]]></add>
    </operation>
    <operation>
        <search position="before"><![CDATA[
        if (isset($this->request->post['mpn'])) {
        ]]></search>
        <add><![CDATA[
        if (isset($this->request->post['source_code'])) {
             $data['source_code'] = $this->request->post['source_code'];
            } elseif (!empty($product_info)) {
             $data['source_code'] = $product_info['source_code'];
            } else {
             $data['source_code'] = '';
        }
        ]]></add>
    </operation>
</file>
<file name="admin/view/template/catalog/product_form.tpl">
    <operation>
        <search position="after"><![CDATA[
        <div class="tab-pane" id="tab-data">
        ]]></search>
        <add><![CDATA[
        <div class="form-group required">
            <label class="col-sm-2 control-label" for="input-source_code">
                 <span data-toggle="tooltip" title="<?php echo $help_source_code; ?>"><?php echo $entry_source_code; ?></span>
             </label>
             <div class="col-sm-10">
                <input type="text" name="source_code" value="<?php echo $source_code; ?>" id="input-source_code" placeholder="<?php echo $entry_source_code; ?>" class="form-control" />
             </div>
        </div>
        ]]></add>
    </operation>
</file>
<file name="admin/language/english/catalog/product.php">
    <operation>
        <search position="after"><![CDATA[
        $_['entry_recurring']        = 'Recurring Profile';
        ]]></search>
        <add><![CDATA[
        $_['entry_source_code']        = 'Source Code';
        ]]></add>
    </operation>
    <operation>
        <search position="after"><![CDATA[
        $_['help_tag']               = 'comma separated';
        ]]></search>
        <add><![CDATA[
        $_['help_source_code']        = 'ACS Billing Code';
        ]]></add>
    </operation>
</file>

Solution

  • Its a very small mistake in your VQmod file.

    Change
    source_code = '" . (int)$data['source_code'] . "'

    To
    source_code = '" . $this->db->escape($data['source_code']) . "'

    Thats it.

    Edit

    As (int) will convert string into the integer. So your input 408000-025 will be 408000