Hi guys,
I have implemented a function to handle the SEO Keyword even if you havent put in the field.
It also take care of existing keywords, as if two product have same SEO keyword in Opencart, it'll mess up all the urls.
So please feel free to comment/suggest/modify:
Showing for Product can be added to Category and Manufacturers as well
STEP1: Open admin/model/catalog/product.php
in function addProduct($data)
Find this:
Code: Select all
if ($data['keyword']) {
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}
at around line 83
Replace it to:
Code: Select all
if ($data['keyword']) {
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->checkExistKeywords($data['keyword']) . "'");
}else{
$data['keyword'] = str_replace(' ','-',$data['product_description'][1]['name']);
$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" . $this->checkExistKeywords($data['keyword']) . "'");
}
Do samething for function editProduct($product_id,$data) at around line 203
STEP2: ok we have changed the base code, now need to create a function which we have added to the above lines
Code: Select all
public function checkExistKeywords($keyword,$counter=1){
$sql = "SELECT keyword from " . DB_PREFIX . "url_alias WHERE `keyword` = '" . $keyword . "'";
$query = $this->db->query($sql);
$result = $query->num_rows;
if($result){
$key_split = explode('-',$keyword);
$key_split_count = count($key_split);
if(is_numeric($key_split[$key_split_count-1])){
$keyword = str_replace('-'.$key_split[$key_split_count-1],'-'.($result+$counter),$keyword);
$keyword = $this->checkExistKeywords($keyword,$result+$counter);
}else{
$keyword = $keyword.'-'.($result+$counter);
$keyword = $this->checkExistKeywords($keyword,$result+1);
}
}
return $keyword;
}
This function check wheather the keyword entered or created does exist or not, if do then change it's name to something like:
keyword-1, it'll recurssively check for each keyword existence in the database and when it gets unique then only it'll return it.
Please let me know if some one has better way to do it.
Thanks
Ash