Skip navigation

Tag Archives: opencart

in your admin/controller/module/[module_name].php

find the line where the document title is set. it looks something like:

1
$this->document->title = $this->language->get('heading_title');

and change it to

1
$this->document->setTitle($this->language->get('heading_title'));

there could be more to this, but worth a try.

opencart has built in seo friendly url but that’s for products only as far as i can tell. and you have to actually type out the terms. so i took it upon myself to make it friendlier and more automagic.

this is for opencart v1.4.8b

the format of url we are trying to achieve is:

/product/:product_id/:product_title

so it would look something like /product/1/girls-gone-wild

change catalog/model/tool/seo_url.php

UPDATED: includes all urls now and added file for download (Catalog.rar (601))

  1. public function rewrite($link)
  2. {
  3. if ($this->config->get(‘config_seo_url’))
  4. {
  5. $url_data = parse_url(str_replace(‘&’, ‘&’, $link));
  6.  
  7. $url = ;
  8.  
  9. $data = array();
  10.  
  11. parse_str($url_data[‘query’], $data);
  12.  
  13. switch($data[‘route’])
  14. {
  15. case ‘product/product’:
  16. $this->load->model(‘catalog/product’);
  17. $product = $this->model_catalog_product->getProduct($data[‘product_id’]);
  18. //get product categories
  19. $path = ;
  20. if(!isset($this->request->get[‘path’]) || empty($this->request->get[‘path’]))
  21. {
  22. $categories = $this->model_catalog_product->getCategories($product[‘product_id’]);
  23. if(!empty($categories))
  24. {
  25. $path = $this->model_catalog_product->getPath($categories[’0′][‘category_id’]);
  26. $path = substr($path,-1,1)==‘_’?substr($path,0,strlen($path)-1):$path;
  27. }
  28. }
  29. else
  30. {
  31. $path = $this->request->get[‘path’];
  32. }
  33. $url = $this->config->get(‘config_url’).‘product/’.$data[‘product_id’].‘/’.(!empty($path)?$path.‘/’:).$this->toSlug($product[‘name’]);
  34. break;
  35. case ‘product/category’:
  36. $this->load->model(‘catalog/category’);
  37. $this->load->model(‘catalog/product’);
  38. $categoryId = explode(‘_’,$data[‘path’]);
  39. $path = ;
  40. if(count($categoryId) == 1)
  41. {
  42. $path = $this->model_catalog_product->getPathUp($categoryId[’0′]);
  43. $path = substr($path,-1,1)==‘_’?substr($path,0,strlen($path)-1):$path;
  44. }
  45.  
  46. $categoryId = $categoryId[(count($categoryId)-1)];
  47. $category = $this->model_catalog_category->getCategory($categoryId);
  48. $url = $this->config->get(‘config_url’).‘category/’.($path?$path:$data[‘path’]).‘/’.$this->toSlug($category[‘name’]);
  49. break;
  50. case ‘information/information’:
  51. $this->load->model(‘catalog/information’);
  52. $info = $this->model_catalog_information->getInformation($data[‘information_id’]);
  53. $url = $this->config->get(‘config_url’).‘information/’.$data[‘information_id’].‘/’.$this->toSlug($info[‘title’]);
  54. break;
  55. default:
  56. $route = explode(‘/’,$data[‘route’]);
  57. $url = $this->config->get(‘config_url’).$data[‘route’];
  58.  
  59. break;
  60. }
  61. if(!empty($url))
  62. {
  63.  
  64. unset($data[‘route’],$data[‘path’],$data[‘product_id’],$data[‘information_id’]);
  65. $query = ;
  66.  
  67. if ($data)
  68. {
  69. foreach ($data as $key => $value)
  70. {
  71. $query .= ‘&’ . $key . ‘=’ . $value;
  72. }
  73.  
  74. if ($query)
  75. {
  76. $query = ‘?’ . trim($query, ‘&’);
  77. }
  78. }
  79. return $url.$query;
  80. }
  81. return $link;
  82.  
  83. } else
  84. {
  85. return $link;
  86. }
  87. }
  88.  
  89. public function toSlug($name)
  90. {
  91. $name = str_replace("’", "", $name);
  92. $name = str_replace(‘"’, "", $name);
  93. $name = strtolower($name);
  94. $name = preg_replace("/&#?[a-z0-9]+;/i","",$name);
  95. $name = preg_replace(‘/[^a-zA-Z0-9-]/’, ‘-’, $name);
  96. $name = preg_replace(‘/-+/’, "-", $name);
  97. return $name;
  98. }

then change catalog/controller/common/seo_url.php

  1. public function index()
  2. {
  3. if (isset($this->request->get[‘_route_’]))
  4. {
  5. $parts = explode(‘/’, $this->request->get[‘_route_’]);
  6. $routes = array(‘category’,‘product’,‘information’,‘account’,‘common’,‘checkout’);
  7.  
  8. if(in_array($parts[0], $routes))
  9. {
  10. switch($parts[0])
  11. {
  12. case ‘category’:
  13. $this->request->get[‘path’] = $parts[1];
  14. break;
  15. case ‘product’:
  16. $this->request->get[‘product_id’] = $parts[1];
  17. $this->request->get[‘path’] = $parts[2];
  18. break;
  19. case ‘information’:
  20. $this->request->get[‘information_id’] = $parts[1];
  21. break;
  22. default:
  23. $this->request->get[‘route’] = $this->request->get[‘_route_’];
  24. break;
  25. }
  26. }
  27. else
  28. {
  29. foreach ($parts as $part)
  30. {
  31. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = ‘" . $this->db->escape($part) . "’");
  32.  
  33. if ($query->num_rows)
  34. {
  35. $url = explode(‘=’, $query->row[‘query’]);
  36.  
  37. if ($url[0] == ‘product_id’)
  38. {
  39. $this->request->get[‘product_id’] = $url[1];
  40. }
  41.  
  42. if ($url[0] == ‘category_id’)
  43. {
  44. if (!isset($this->request->get[‘path’]))
  45. {
  46. $this->request->get[‘path’] = $url[1];
  47. } else
  48. {
  49. $this->request->get[‘path’] .= ‘_’ . $url[1];
  50. }
  51. }
  52.  
  53. if ($url[0] == ‘manufacturer_id’)
  54. {
  55. $this->request->get[‘manufacturer_id’] = $url[1];
  56. }
  57.  
  58. if ($url[0] == ‘information_id’)
  59. {
  60. $this->request->get[‘information_id’] = $url[1];
  61. }
  62. } else
  63. {
  64. $this->request->get[‘route’] = ‘error/not_found’;
  65. }
  66. }
  67. }
  68.  
  69. if (isset($this->request->get[‘product_id’]))
  70. {
  71. $this->request->get[‘route’] = ‘product/product’;
  72. } elseif (isset($this->request->get[‘path’]))
  73. {
  74. $this->request->get[‘route’] = ‘product/category’;
  75. } elseif (isset($this->request->get[‘manufacturer_id’]))
  76. {
  77. $this->request->get[‘route’] = ‘product/manufacturer’;
  78. } elseif (isset($this->request->get[‘information_id’]))
  79. {
  80. $this->request->get[‘route’] = ‘information/information’;
  81. }
  82.  
  83. if (isset($this->request->get[‘route’]))
  84. {
  85. return $this->forward($this->request->get[‘route’]);
  86. }
  87. }
  88. }