Opencart SEO URL on new extensions

 

On custom Opencart extension development if we want to use the native SEO URL method, for example using the following code to generate the URL, and catch the request for it, doing the apropriate routing.

$this->url->link(‘forder/file’, ‘ext_id=’ . $ext_id)

in our custom extensions we need to:

  • Store the URL SEO on extension model
  • Change the Opencart controller seo_url.php

 

Store the URL SEO in Opencart custom extension model

On the extension model we need to store the query and the keyword.

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

 

Change the SEO Opencart controller to handle the extension

Change the Opencart controller file that handle the SEO URL job to be aware of your extension SEO URL.

On file: catalog/controller/common/seo_url.php

Handle requests with SEO urls

To get our item id from database based on keyword (URL text).

After:

if ($url[0] == 'information_id') {
 $this->request->get['information_id'] = $url[1];
}

Add:

// bof hack
if ($url[0] == 'ext_id') {
 $this->request->get['ext_id'] = $url[1];
}
// eof hack

To get our route base on kind of item id.

After:

} elseif (isset($this->request->get['information_id'])) {
 $this->request->get['route'] = 'information/information';

Add:

// bof hack: add new_id
} elseif (isset($this->request->get['new_id'])) {
 $this->request->get['route'] = 'information/ext';
// eof hack: add new_id

 

Generate the SEO urls

To get Opencart generate the SEO URL for your extension links

Change the line:

if ($query->row['query'] && $url[0] != 'information_id' && $url[0] != 'manufacturer_id' && $url[0] != 'category_id' && $url[0] != 'product_id') {

To

if ($query->row['query'] && $url[0] != 'information_id' && $url[0] != 'manufacturer_id' && $url[0] != 'category_id' && $url[0] != 'product_id' && $url[0] != 'ext_id') {

And its done.

Tested on: Opencart 2.0.3.1