Ketan Patel    15-Sep-2013

In this tutorial we will see how to add meta description and meta keywords in opencart. Just follow the steps given below.

 

Step 1 : Add new fields "Meta Keywords" and "Meta Description" in the database

In order to add Meta Keywords and Meta Description in pages we first have to add Meta Description and Meta Keywords  fields into the database in the table called as information_description. Simply Run the following query:

ALTER TABLE `information_description` ADD `meta_description` VARCHAR( 255 ) NOT NULL, ADD `meta_keywords` VARCHAR( 255 ) NOT NULL

 

Note: please consider if you have database prefix or not. (Example: oc_) if so then use "oc_information_description.

 

Step 2 : Add two Text boxes in admin panel

Go to : admin/view/template/catalog/information_form.tpl file and search for the $entry_description code and insert the code given below just before it:

<tr>
   <td><?php //echo $entry_meta_description;?>Meta Description</td>
   <td><textarea  name="information_description[<?php  echo $language['language_id'];?>][meta_description]" cols="50"  rows="6"><?php echo isset($information_description[$language['language_id']]) ? $information_description[$language['language_id']]['meta_description'] : ''; ?>
</textarea>
    </td>
</tr>
<tr>
    <td><?php //echo $entry_meta_keywords; ?>Meta Keywords</td>
    <td><textarea  name="information_description[<?php  echo $language['language_id'];?>][meta_keywords]" cols="50"  rows="6"><?php echo isset($information_description[$language['language_id']]) ? $information_description[$language['language_id']]['meta_keywords'] : ''; ?>
  </textarea>
   </td>
</tr>

 

Step:3 Add new fields in the model

Go to admin/model/catalog/information.php. Search for below code  in the addInformation() function.

$this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "'");

Replace it with  code given below:

$this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', meta_description = '".$this->db->escape($value['meta_description'])."', meta_keywords = '".$this->db->escape($value['meta_keywords'])."'");

 

Now, in the same function addInformation(); search for this code,

if ($data['keyword']) {
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'information_id=" . (int)$information_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}

Replace it with code given below:

if ($data['keyword']) { $this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', meta_description = '".$this->db->escape($value['meta_description'])."', meta_keywords = '".$this->db->escape($value['meta_keywords'])."'"); }

 

Now find editInformation() function and search the following code,

$this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "'");

Replace the code with  code given below:

$this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', meta_description = '".$this->db->escape($value['meta_description'])."', meta_keywords = '".$this->db->escape($value['meta_keywords'])."'");

 

Now in getInformationDescriptions() function search for the below code.

foreach ($query->rows as $result) {
    $information_description_data[$result['language_id']] = array(
        'title'       => $result['title'],
        'description' => $result['description']
    );
}

Replace it with code given below.

foreach ($query->rows as $result) {
    $information_description_data[$result['language_id']] = array(
        'title'            => $result['title'],
        'description'      => $result['description'],
        'meta_description' => $result['meta_description'],
        'meta_keywords'    => $result['meta_keywords']
    );
}

 

Open /catalog/controller/information/information.php file and in index() function search for the code

if ($information_info) {
$this->document->setTitle($information_info['title']);

Simply add the following two lines just below it.

$this->document->setDescription($information_info['meta_description']);
$this->document->setKeywords($information_info['meta_keywords']);

Now it will look like this,

if ($information_info) { 
$this->document->setTitle($information_info['title']); 
$this->document->setDescription($information_info['meta_description']); 
$this->document->setKeywords($information_info['meta_keywords']);

 

Final Step :
Go to admin side and open catalog > information and open any page. You will find two new fields Meta Description and Meta Keywords are added. Enter the details and go to front side and check the page source by pressing Ctrl+U. You will find entered details within Meta Description and Meta Keywords tags.


 

About Ketan Patel

A Web Developer having 7+ years of experience in web development. Worked with Laravel-Lumen, CakePHP, CodeIgniter, Opencart to make sites for startup companies and small businesses. Apart from blogging, he likes to read Novels, Listening music and Net surfing.