Post by Microbe » Sun Apr 15, 2012 8:01 am

I want to do an automatic replace of spaces and a few other characters e.g. ?, &, / etc in the SEO keyword field when products are created or edited.

This should happen by default but it doesn't.

Can someone point me to where the insert or update for products happens? I can't track it down.

Thanks in advance

Newbie

Posts

Joined
Tue Nov 01, 2011 9:36 am

Post by Avvici » Sun Apr 15, 2012 8:05 am

As far as index.php?route=, that is already replaced. If you see a / in an SEO url it is simply separating directories. The - that you speak of can be entered manually where you type in the SEO keyword for that product or category.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by Microbe » Sun Apr 15, 2012 8:11 am

Of course it can be entered manually, but if copying the product description and pasting it as SEO it is unnecessary, that is what clever coding is for.

Newbie

Posts

Joined
Tue Nov 01, 2011 9:36 am

Post by Avvici » Sun Apr 15, 2012 8:25 am

Open Cart uses APACHE MOD REWRITE well.....not sure what the problem is.
We all here are willing to learn this "clever coding" Why not elaborate a little and explain it to us.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by Microbe » Sun Apr 15, 2012 8:37 am

:)

OK, I'll explain the problem slowly.

If a person has a product named "this is my fabulous product" and they copy and paste it as the SEO keyword it becomes "this%20is%20my%20fabulous%20product". I think we agree that "this-is-my-fabulous-product" is preferable.

So instead of having to manually replace the spaces etc with a - on save (or update) a clever and simple piece of code would be something like

strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $str), '-'));

That's the easy part. Where to put it is what the question was.

Newbie

Posts

Joined
Tue Nov 01, 2011 9:36 am

Post by Avvici » Sun Apr 15, 2012 8:51 am

Okay I understand now. Yes, that would make sense if you want the "-" as the seperator
That code would go in the model file prior to the INSERT query. Obviously you want to set up a check for strings that have the extra space, and }ELSE{ to the default $data['keyword'] if not.

Here is your variable: $data['keyword']
File: admin/model/catalog/product.php

Code: Select all

$new_seo_keyword = strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $str), $data['keyword']));
Insert:

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($new_seo_keyword) . "'");
		}
You sound like you know PHP just fine. Get it done!
Last edited by Avvici on Sun Apr 15, 2012 8:53 am, edited 1 time in total.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by Microbe » Sun Apr 15, 2012 9:14 am

Thanks almost perfect.

In fact rather than meta-keyword, it was just keyword and it ended up being

Code: Select all

$this->db->escape($data['keyword'])
to

Code: Select all

$this->db->escape(strtolower(trim(preg_replace('/[^a-zA-Z0-9]+/', '-', $data['keyword']), '-')))
in 2 places

Hope this helps someone else AND it should be considered for future releases

Newbie

Posts

Joined
Tue Nov 01, 2011 9:36 am

Post by Avvici » Sun Apr 15, 2012 9:27 am

This is good only if you plan on all of your SEO ALIASES to have spaces between words. That's why I mentioned setting up a conditional to check for that, ELSE insert something else :)

Good luck

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC
Who is online

Users browsing this forum: Majestic-12 [Bot], Semrush [Bot] and 7 guests