Post by Kartoffelz » Fri Aug 12, 2011 4:51 pm

Hello all,

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;
}
What does this do? It removes all 'illegal' characters that are considered 'illegal' by searcheninges. Plus, the spaces, underscores, plus, @-signs, inches, will be tactically replaced by an '-'. All inappropriate -'s at the beginning and/or end of a string will be trimmed, to not overdo the dashes. :)
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']) . "'");
}
Add the following, right behind it:

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']) . "'");
}
What does this do? In case if nothing has been specified in the SEO-keyword inputfield, it will make a SEO-friendly URL itself, using the newly created function and inserts it into the URL_alias table - this way, you'll have a smooth category-URL.
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']) . "'");
}
Don't try to use the same code as category, since this will result in a blank URL - no category description has been given, a product description now. ;)
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']) . "'");
}
It's a small difference, it will now use the seoURL-function, no matter what.

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
Last edited by Kartoffelz on Sat Jan 21, 2012 11:16 pm, edited 2 times in total.

New member

Posts

Joined
Fri Apr 04, 2008 10:44 pm
Location - The Netherlands

Post by Ruby815 » Fri Aug 12, 2011 7:21 pm

Thanks for this detailed info, I was looking for this kind of help...

making money online


Newbie

Posts

Joined
Fri Aug 12, 2011 7:18 pm

Post by Maansy » Sat Aug 13, 2011 1:08 am

Thanks. Now vqmod it and call it a day ;)
Will it auto generate SEO keywords for the rest of the store pages?

ALL Templates :: 1.5.1+ Templates :: 50%-75% PRICE DROP ONLY at OpencartStuff.com


User avatar
Active Member

Posts

Joined
Thu Jun 24, 2010 6:04 am


Post by Kartoffelz » Sat Aug 13, 2011 2:57 am

Maansy wrote:Thanks. Now vqmod it and call it a day ;)
Will it auto generate SEO keywords for the rest of the store pages?
No. Only for the categories and products. I have not seen into the rest of the pages, but I assume that the slightest modification to that should do the trick. ;)
I have no experience with vqmod (yet) - so who knows...

New member

Posts

Joined
Fri Apr 04, 2008 10:44 pm
Location - The Netherlands

Post by Kartoffelz » Sat Aug 13, 2011 5:28 pm

I've added the "$language_id"-variable. This is done since I changed language and then the form changes as well, so to always stay up to date with languages, I've added this variable. It has been edited in the original post. :)

New member

Posts

Joined
Fri Apr 04, 2008 10:44 pm
Location - The Netherlands

Post by harryo40 » Fri Jan 20, 2012 9:04 pm

Kartoffelz wrote:
Maansy wrote: I have no experience with vqmod (yet) - so who knows...
Hope you dont mind but as I found this to be very useful indeed & it saves us a lot of time, I have created a vQmod version of it, so that others can use it without altering the code ;)
It can be downloaded here: http://www.opencart.com/index.php?route ... on_id=4709
cheers
harryo

Add To Cart Confirm Ajax Popup for OC 1.5.1.3 --> 1.5.5.1 - Add to Cart Confirmation Popup
Image Map Banner Module - Image Map Banner Module
----------------------------------------------------------------------
Womens Famous Name Fashion Clothing at bargain prices - Next2nowt.com


Active Member

Posts

Joined
Wed Oct 21, 2009 3:37 am
Location - Blackburn, Lancashire

Post by Kartoffelz » Fri Jan 20, 2012 11:45 pm

harryo40 wrote:Hope you dont mind but as I found this to be very useful indeed & it saves us a lot of time, I have created a vQmod version of it, so that others can use it without altering the code ;)
It can be downloaded here: http://www.opencart.com/index.php?route ... on_id=4709
cheers
harryo
That's great! Thanks for putting me in the description. Hopefully this way we can help some people, even better. ;D

New member

Posts

Joined
Fri Apr 04, 2008 10:44 pm
Location - The Netherlands
Who is online

Users browsing this forum: No registered users and 9 guests