Post by opencart-templates » Wed Jun 01, 2011 3:08 am

This simple few lines of jQuery magic that auto generates a search engine friendly keyword in the product, category, manufacturer and information keyword used in the URL.

I have wrote this to only work when you first create a product/etc because you don't want the URL changing every time you change the name.

The easiest place to add this is: /admin/view/template/common/header.tpl
Find the

Code: Select all

$(document).ready(function(){
Add this below:

Code: Select all

var keyword = $("input[name=keyword]");
if(!keyword.val()){
   $("input[name^='product_description'],input[name^='category_description'],input[name='name'],input[name^='information_description'],input[name^='blog_description'],input[name^='blog_category_description'],input[name^='calendar_description']").keyup(function(){
	  var SEOlink = $(this).val();
	  
	  if(keyword.length <= 50){
		 SEOlink = SEOlink.replace(/^\s+|\s+$/g, ''); // trim
		 SEOlink = SEOlink.toLowerCase();
	  /* remove accents, swap, etc
	  var from = "àáäâèéëêìíïîòóöôùúüûñcçcllštžýnrrd·/_,:;";
	  var to   = "aaaaeeeeiiiioooouuuuncccllstzynrrd------";
	  for (var i=0, l=from.length ; i<l ; i++) {
		 SEOlink = SEOlink.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
	  }*/
	  SEOlink = SEOlink.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
	  .replace(/\s+/g, '-') // collapse whitespace and replace by -
	  .replace(/-+/g, '-'); // collapse dashes
	  // return SEOlink;
	  keyword.val(SEOlink);
	  }       
   }).trigger("keyup");
}

Or if you have vQmod installed download:
Last edited by opencart-templates on Thu May 03, 2012 10:03 pm, edited 4 times in total.

Advanced Professional Email Template
Customers Pre-Sale. Inc abandoned cart email
Order Follow-Up Email. Inc request review
Email Validation with ZeroBounce


User avatar
Active Member

Posts

Joined
Mon May 16, 2011 7:24 pm
Location - UK

Post by iidx036 » Wed Jun 22, 2011 6:58 pm

Hi,

Could you be more specific in where this code needs to go?

regards,

Jay

Newbie

Posts

Joined
Tue Jun 14, 2011 5:50 pm

Post by opencart-templates » Wed Jun 29, 2011 10:42 pm

Sadly there is no default javascript file for the admin area. So you can either add this for each product/category/information form or to the admin/view/template/common/header.tpl find $(document).ready

Advanced Professional Email Template
Customers Pre-Sale. Inc abandoned cart email
Order Follow-Up Email. Inc request review
Email Validation with ZeroBounce


User avatar
Active Member

Posts

Joined
Mon May 16, 2011 7:24 pm
Location - UK

Post by Aco » Fri Sep 09, 2011 2:12 pm

Here's my quick improvment:
  • Removes trailing dashes
    Doesn't add dashes for removed punctuation

Code: Select all

text = text.toLowerCase();
text = text.replace(/[^a-zA-Z0-9 ]+/g,'');
text = $.trim(text);
text = text.replace(/[ ]+/g,'-');

Aco
Newbie

Posts

Joined
Fri Sep 09, 2011 2:08 pm
Location - UK

Post by Aco » Fri Sep 09, 2011 2:24 pm

Also, I've added an extra selector to work for information pages (useful for delivery information, T&Cs etc).

Full code:

Code: Select all

var keyword = $("input[name=keyword]").val();
if (!keyword) {
$("input[name^='product_description'],input[name^='category_description'],input[name='name'],input[name='information_description[1][title]']").keyup(function(){
    var text = $(this).val();
    text = text.toLowerCase();
    text = text.replace(/[^a-zA-Z0-9 ]+/g,'');
    text = $.trim(text);
    text = text.replace(/[ ]+/g,'-');
    $("input[name=keyword]").val(text);       
 });
}

Aco
Newbie

Posts

Joined
Fri Sep 09, 2011 2:08 pm
Location - UK

Post by aethertrebs » Tue Oct 25, 2011 11:30 pm

Thank you so much. This bit of code should be put into the next release. (Maybe checking it against the DB first)

Newbie

Posts

Joined
Tue Oct 25, 2011 11:29 pm

Post by labeshops » Tue Nov 22, 2011 9:59 pm

great timesaver! Question though. I add my model at the end of the seo keywords to avoid possible duplication since I sell so many products. Is it possible to add this by default?

Running Opencart v3.0.3.2 with multi-stores from https://www.labeshops.com which has links to all my stores.

Image


User avatar
Expert Member

Posts

Joined
Thu Aug 04, 2011 4:41 am
Location - Florida, USA

Post by Aco » Thu Nov 24, 2011 2:16 pm

@aethertrebs - Yes, an AJAX DB lookup to check for duplicates would be nice, especially as opencart doesn't validate this by default either.

@labeshops - should be pretty easy to modify, you'd probably just need to change the line

Code: Select all

var text = $(this).val();
to

Code: Select all

var text = $(this).val() + ' ' + $("input[name='model']").val();
and slim down the list of sections that it applies to in the third line...

Aco
Newbie

Posts

Joined
Fri Sep 09, 2011 2:08 pm
Location - UK

Post by labeshops » Fri Nov 25, 2011 11:05 pm

That didn't work. It appears it doesn't go back and add the model number after you put in the title.

Running Opencart v3.0.3.2 with multi-stores from https://www.labeshops.com which has links to all my stores.

Image


User avatar
Expert Member

Posts

Joined
Thu Aug 04, 2011 4:41 am
Location - Florida, USA
Who is online

Users browsing this forum: No registered users and 2 guests