Add Meta Description and Meta Keywords in opencart



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']) . "'");

and 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']
            );
        }

Step:4 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 withing meta description and Meta keywords tags.

Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP