Hello Friends,
I hope you all are doing very well :)
As you all know that by default Opencart not provide an option for add to "Meta Title". There are some SEO opencart modules aveliable for add to "Meta Title" option but almost they are paid. I am here given you steps by steps instruction to add new meta title fields on product pages by edit in exist files, please follow given below steps:
First of all you will need to add new meta_title fields under "oc_product_description" table into your database. You will need to run given below mysql query.
ALTER TABLE oc_product_description ADD meta_title VARCHAR(100) NOT NULL
Now you will need to define text of meta title into catalog language file
admin/language/english/catalog/product.php
Search $_['entry_meta_keyword'] = 'Meta Tag Keywords:'; (around line no. 30) and add
$_['entry_meta_title'] = 'Meta Title:';
Now you will need to add Meta Title field into product add/edit form
admin/view/template/catalog/product_form.tpl
Seach "$entry_meta_description" (around line no. 30) and add mew row
<tr>
<td><?php echo $entry_meta_title; ?></td>
<td><input type="text" name="product_description[<?php echo $language['language_id']; ?>][meta_title]" size="100" value="<!--?php echo isset($product_description[$language['language_id']]) ? $product_description[$language['language_id']]['meta_title'] : ''; ?>" />
</td>
</tr>
In next step you will need to update catalog controller file
/admin/controller/catalog/product.php
Search
$this->data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');
(around line no. 542)
add above OR below
$this->data['entry_meta_title'] = $this->language->get('entry_meta_title');
In next step you will need to update catalog model file
/admin/model/catalog/product.php
Under addProduct() function find foreach loop
foreach ($data['product_description'] as $language_id => $value)
(around line no. 15) and add new meta title filed into mysql query.
foreach ($data['product_description'] as $language_id => $value)
{
$legend_data = $value['legend_origin']."|".$value['legend_material']."|".$value['legend_stackable']."|".$value['legend_assemble']."|".$value['legend_outdoor']."|".$value['legend_warranty'];
$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', legends = '".$this->db->escape($legend_data)."', colour_info = '".$this->db->escape($value['colour_info'])."'");
}
Now under editProduct() function search foreach ($data['product_description'] as $language_id => $value) { loop (around line no. 132)
updated foreach loop will be look like this
foreach ($data['product_description'] as $language_id => $value) {
$legend_data = $value['legend_origin']."|".$value['legend_material']."|".$value['legend_stackable']."|".$value['legend_assemble']."|".$value['legend_outdoor']."|".$value['legend_warranty'];
$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET product_id = '" . (int)$product_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', description = '" . $this->db->escape($value['description']) . "', tag = '" . $this->db->escape($value['tag']) . "', legends = '".$this->db->escape($legend_data)."', colour_info = '".$this->db->escape($value['colour_info'])."'");
}
Now under getProductDescriptions() function search foreach ($query->rows as $result) { loop (around line no. 410) and add meta_title field into this
updated loop will be look like this
foreach ($query->rows as $result) {
if ($result['legends'] != "")
{
$legend_data = explode("|", $result['legends']);
$product_description_data[$result['language_id']] = array(
'name' => $result['name'],
'description' => $result['description'],
'meta_title' => $result['meta_title'],
'meta_keyword' => $result['meta_keyword'],
'meta_description' => $result['meta_description'],
'tag' => $result['tag'],
'legend_origin' => $legend_data[0],
'legend_material' => $legend_data[1],
'legend_stackable' => $legend_data[2],
'legend_assemble' => $legend_data[3],
'legend_outdoor' => $legend_data[4],
'legend_warranty' => $legend_data[5],
'colour_info' => $result['colour_info']
);
}
else
{
$product_description_data[$result['language_id']] = array(
'name' => $result['name'],
'description' => $result['description'],
'meta_title' => $result['meta_title'],
'meta_keyword' => $result['meta_keyword'],
'meta_description' => $result['meta_description'],
'tag' => $result['tag'],
'legend_origin' => "",
'legend_material' => "",
'legend_stackable' => "",
'legend_assemble' => "",
'legend_outdoor' => "",
'legend_warranty' => "",
'colour_info' => $result['colour_info']
);
}
}
Now you have done all work for admin section, you can test it by edit/add any product/category
Lets now implement meta title into front-end
/catalog/model/catalog/product.php
Search getProduct() function (around line no.7) and add new field meta_title into MySql select query.
seach "meta_description" and add new meta_title field in same way into return array.
shop/catalog/controller/product/product.php
Under index() function search $this->document->setTitle($product_info['name']); (around line no. 222) and replaced this line with given below code
if(isset($product_info['meta_title']) && $product_info['meta_title']!='')
{
$metatitle= $product_info['meta_title'];
}
else
{
$metatitle=$product_info['name']; // set by default product title as meta title
}
$this->document->setTitle($metatitle);
That's all!!
One Reply to “How to add Meta Title field on product pages: Opencart”