Recently I started using Opencart again and figured out I still have an account here. I like the overall setting of Opencart, but I do find it annoying and less user-friendly that the user (admin) has to manually think of a SEO-keyword.Therefore, I decided that a small modification could easily help me, if I wanted to populate the database with products and categories and yet prefer they all get URL-aliases. It works nicely on version 1.5.1.1, so hereby my first and small contribution to the Opencart-community!

VQmod-version:
It has been converted for the VQmod. Check it out!
http://www.opencart.com/index.php?route ... on_id=4709
Files
/system/library/url.php
/admin/model/catalog/product.php
/admin/model/catalog/category.php
URL.php
Here I add a selfmade "SEO-urlgenerator" (good word for Scrabble one day). Go to line 39, where the last public function rewrite() ends and add the following, still all in the URL-class:
Code: Select all
public function seoURL($str) {
$str = preg_replace('/[^a-zA-Z0-9]+/', '-', $str);
$str = trim($str, '-');
$str = strtolower($str);
return $str;
}

Save the file and be sure to upload it.
category.php
In version 1.5.1.1 search around line 30, where you'll find this code:
Code: Select all
if ($data['keyword']) {
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}
Code: Select all
elseif(empty($data['keyword'])) {
$data['keyword'] = $this->url->seoURL($data['category_description'][$language_id]['name']);
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}
Side-note for the experts: I've used the first data of the array given in the description. This shouldn't matter, since the URL aren't determined or stored separately for different languages.
Of course, you also might want to introduce this technique for editing categories. Find around line 79, the same line, with the same exact code and replace it.
product.php
Now it's getting simple. Just do the same trick with the product-model. Around line 114, find a smiliar looking line of code. And add the following:
Code: Select all
elseif(empty($data['keyword'])) {
$data['keyword'] = $this->url->seoURL($data['product_description'][$language_id]['name']);
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}

For the edit function, go to line 267 - where once again, you'll find the same code of line. Add the code provided above as well, to make sure the same trick is applied when editing products.
Now, be noted that once you edit a products name and the URl-alias has already been created, it will NOT use the seoURL-function. I would highly recommend adding the seoURL-function IF something has been specified for categories or products. It takes away your 'freedom' of using custom made SEO-keywords, but it will always provide you a proper and related URL-alias. I've applied this for myself, since I don't want to struggle filling in this inputfield all the time. So the code of category.php shows for me:
Code: Select all
if ($data['keyword']) {
$data['keyword'] = $this->url->seoURL($data['category_description'][$language_id]['name']);
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}
elseif(empty($data['keyword'])) {
$data['keyword'] = $this->url->seoURL($data['category_description'][$language_id]['name']);
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}
I hope I've been able to contribute a little to the community and people find this helpful. It was a personal struggle and irritation, so I've came up with my own solution. I figured, since I am a pragmatic person, that more people might find this annoying.
Anyway, enjoy your day!

Aut viam inveniam, aut faciam--Latin poverb