Today I want to show you how to create new product field in back office on product edit page (informations tab). Of course you will be able to fill field with own value and this value will be stored in database. This tutorial is an answer for a Dean request. He is a owner of dekom.co.rs store. Here is his request:
What I need is another text field which I can add to a product. This text field should be similar to a field where you put the product name.
To achieve what he expect it will be necessary to modify several things. Hope that this tutorial will be helpful also for other merchants with similar needs. This tutorial is a great "pattern" for other modifications related to own fields in back office. So we want to create field named: internal name. Of course you can use your own name.
Step 1
The first step is related to database. We need to create new field in ps_product table. so, open phpmyadmin and alter ps_product table. Add new VARCHAR(200) field (i think that 200 chars for internal name are enough, am i right?) field name: internal_name, specification:
Internal name field in database, ps_product table (click to enlarge)
Step 2
Now it's time to extend product object, in this case we need to modify core files. Open classes/product.php file, you can find there code like:
class ProductCore extends ObjectModel { /** @var string Tax name */ public $tax_name; /** @var string Tax rate */ public $tax_rate; ... ... ...
add there new field definition:
/** @var string Tax rate */ public $internal_name;
After modifications our code should looks like: (i highlighted added two lines of code)
class ProductCore extends ObjectModel { /** @var string Tax name */ public $tax_name; /** @var string Tax rate */ public $tax_rate; /** @var string Tax rate */ public $internal_name; ... ... ...
We need also to extend object definition: add: 'internal_name' => array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml') to object definition code.
Step 3
Now we must edit back office template file. We must modify product edit .tpl file located in this path: ADMIN_DIR/themes/default/template/controllers/products/informations.tpl file.(ADMIN_DIR is of course your admin directory). What we have to do there? We must add code with our new field. So, below code:
<tr> <td class="col-left"> {include file="controllers/products/multishop/checkbox.tpl" field="name" type="default" multilang="true"} <label>{l s='Name:'}</label> </td> <td style="padding-bottom:5px;" class="translatable"> {foreach from=$languages item=language} <div class="lang_{$language.id_lang}" style="{if !$language.is_default}display: none;{/if} float: left;"> <input class="{$class_input_ajax}{if !$product->id}copy2friendlyUrl{/if} updateCurrentText" size="43" type="text" {if !$product->id}disabled="disabled"{/if} id="name_{$language.id_lang}" name="name_{$language.id_lang}" value="{$product->name[$language.id_lang]|htmlentitiesUTF8|default:''}"/><sup> *</sup> <span class="hint" name="help_box">{l s='Invalid characters:'} <>;=#{}<span class="hint-pointer"> </span> </span> </div> {/foreach} </td> </tr>
add this one:
<tr> <td class="col-left"> <label>{l s='Internal Name:'}</label> </td> <td style="padding-bottom:5px;"> <input type="text" id="internal_name" name="internal_name" value="{$product->internal_name|default:''}"/> </td> </tr>
Code that we added has got definition of our new field. We named this field as you probably remember as "internal_name" - use there your field name :)
Effect
New field on product edit page (click to enlarge)