Hello Friends,
I hope you all are doing very well :)
In my last post https://www.wp-experts.in/blog/2015/11/17/how-add-meta-title-field-on-product-pages-opencart/, i have explain you instruction for add to "Meta Title" on product pages. Now here i am given you steps by steps instruction to add new meta title fields on category 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_category_description" table into your database. You will need to run given below mysql query.
ALTER TABLE oc_category_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/category.php
Search $_['entry_meta_keyword'] = 'Meta Tag Keywords:'; (around line no. 20) and add
$_['entry_meta_title'] = 'Meta Title:';
Now you will need to add Meta Title field into category through add/edit form tpl file
admin/view/template/catalog/category_form.tpl
Search "$entry_meta_description" (around line no. 30) and add mew row
<tr>
<td><?php echo $entry_meta_title; ?></td>
<td><input type="text" name="category_description[<?php echo $language['language_id']; ?>][meta_title]" value="<?php echo isset($category_description[$language['language_id']]) ? $category_description[$language['language_id']]['meta_title'] : ''; ?>" size="100"/></td>
</tr>
In next step you will need to update catalog controller file
/admin/controller/catalog/category.php
Search
$this->data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');
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/category.php
Under addCategory() function search $data['category_description'] foreach loop (around line no. 12) and add new meta title filed into mysql query.Updated loop will be look like this
foreach ($data['category_description'] as $language_id => $value) {
$this->db->query("INSERT INTO " . DB_PREFIX . "category_description SET category_id = '" . (int)$category_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']) . "'");
}
Now under editCategory() function search category description foreach loop (around line no. 66) and replace it with given below updated
foreach ($data['category_description'] as $language_id => $value) {
$this->db->query("INSERT INTO " . DB_PREFIX . "category_description SET category_id = '" . (int)$category_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']) . "'");
}
Now under getProductDescriptions() function search foreach ($query->rows as $result) { loop (around line no. 410) and add meta_titleUnder getCategoryDescriptions() function search foreach loop (around line no. 233)
foreach ($query->rows as $result) {
$category_description_data[$result['language_id']] = array(
'name' => $result['name'],
'meta_keyword' => $result['meta_keyword'],
'meta_description' => $result['meta_description'],
'description' => $result['description']
);
}
and replaced it with given below foreach field
foreach ($query->rows as $result) {
$category_description_data[$result['language_id']] = array(
'name' => $result['name'],
'meta_keyword' => $result['meta_keyword'],
'meta_title' => $result['meta_title'],
'meta_description' => $result['meta_description'],
'description' => $result['description']
);
}
Now you have done all work for admin section, you can test it by edit/add any category
Lets now implement meta title into front-end
shop/catalog/controller/product/category.php
Under index() function search
$this->document->setTitle($category_info['name']);
(around line no. 92) and replaced this line with given below code
if(isset($category_info['meta_title']) && $category_info['meta_title']!='')
{$metatitle= $category_info['meta_title'];}
else{
$metatitle=$category_info['name']; //by default get category title
}
$this->document->setTitle($metatitle);
That's ALL!!
Great Artical