Post by KDNovelties » Sat Jul 01, 2017 10:34 pm

Hi all, I noticed that the canonical link only goes as far as the product page, and doesn't include the category. Is there a way to correct this? Also, how do you se it if a product belongs to multiple categories, is there a "master" category that can be set? Here is an example:

URL reads as: http://domain.com/personalized-books/i- ... abets.html
Which is fine and how we want it to be. However, the canonical link reads as:
<link href="http://domain.com/i-see-do-you-see-alphabets.html" rel="canonical" />
As you can see, the canonical link does not include the category, which is not good.

Any help would be appreciated.
Thanks.

Angel

User avatar
New member

Posts

Joined
Sat May 27, 2017 12:12 am

Post by IP_CAM » Sat Jul 01, 2017 11:12 pm

Well, Products are NOT bould to Master Categories, they can be called
without category names as well. But when looking at this:
i-see-do-you-see-alphabets.html
I assume, that you use a Custom Extension, adding a .HTML ending to the links,
so, you should make sure, to always inform, what Extension you use for such as well.
Ernie

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by KDNovelties » Sun Jul 02, 2017 4:30 am

Thanks, I do not need to use an extension. However, the products should be under a category for proper SEO. You want the category to carry weight in the SERP. Not good with out it. I've seen other OC sites have this. How does this get done. Thanks

User avatar
New member

Posts

Joined
Sat May 27, 2017 12:12 am

Post by thekrotek » Sun Jul 02, 2017 5:23 am

You probably don't understand, how OC categories work. In short, one product can have MULTIPLE categories assigned plus it have a direct link. So in general you have multiple SEO links:

yoursite.com/product1
yoursite.com/category1/product1
yoursite.com/category2/product1
yoursite.com/category3/product1
...

But canonical link is always a direct link to product. And no, product shouldn't always belong to category, because, I repeat, it can have MULTIPLE categories assigned.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by KDNovelties » Sun Jul 02, 2017 5:44 am

Thanks, yes, I do understand that a product can belong to several categories. However, for SEP purposes, it is best to have the canonical link include a category, for example, yoursite.com/SETCATEGORY/product1. SETCATEGORY carries much weight in the SERPs. That would be your key word. Do you mean to tell me this cannot be done in OC? I mean, this has been available in really old versions of CubeCart. There has to be something to get this done.

User avatar
New member

Posts

Joined
Sat May 27, 2017 12:12 am

Post by KDNovelties » Sun Jul 02, 2017 5:52 am

So there is an old version of this:

viewtopic.php?t=45677
This doesn't seem to work for 2.3, Does anyone know of a new ver?

Thanks

User avatar
New member

Posts

Joined
Sat May 27, 2017 12:12 am

Post by thekrotek » Sun Jul 02, 2017 6:00 am

Let's start from the very beginning: product can be assigned to MULTIPLE categories. Considering this, which particular category you want to have in canonical link and by which rules do you want to define it?

It's not right to compare it to another e-commerce solution, because they all have different design. For example, CubeCart might has a primary category and in this case adding it to canonical link or even displaying it in every link isn't a problem. Can't say the same about OC, because it's designed differently.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by KDNovelties » Sun Jul 02, 2017 7:05 am

I would want to assign it to the parent category. So for example, if I have product1 in the following categories/subcategories:
/books/product1
/books/adventure/product1
/books/boys/product1
/books/educational/product1

I would want it in books, so the canonical link would point to /books/product1. Mind you, from an SEO perspective, having "books" in the canonical link is important, which is why I'm surprised it's not a default feature.

The examples above should be easy to do as it should only have to point to the parent category. Any extension for this? Certainly I cannot be the first to need this.

Thanks
Angel

User avatar
New member

Posts

Joined
Sat May 27, 2017 12:12 am

Post by thekrotek » Sun Jul 02, 2017 3:46 pm

But it can have MULTIPLE parent categories!!! And they all will have EQUAL value!

Not a single category in OpenCart has a higher value comparing to others, so there's no way to define, which category you want to have in canonical link. The only thing you can do is find SEO extension, which allows to define custom canonical links. Or create a VQMod, which will add a particular category for each product separately.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by KDNovelties » Thu Sep 14, 2017 10:19 am

I created an extension to do this. It will change the canonical link for any product to be the parent category. You can also select several product to have a sub category as the canonical link. I hard coded those products into an array since I only had a few. Someone with a lot more knowledge than I can probably make it better.

Angel
KD Novelties.
www.kdnovelties.com

Code: Select all

<modification>
	<id>Canonical Product URL with Category Path</id>
	<version>1.0</version>
	<vqmver>2.1.5</vqmver>
	<author>https://www.kdnovelties.com</author>
	<file name="catalog/controller/product/product.php">
		<operation>
			<search position="replace"><![CDATA[$this->document->addLink($this->url->link('product/product', 'product_id=' . $this->request->get['product_id']), 'canonical');]]></search>
			<add><![CDATA[if($this->model_catalog_product->getCategoryPath($this->request->get['product_id'])!='0'){
    $this->document->addLink($this->url->link('product/product', 'path=' . $this->model_catalog_product->getCategoryPath($this->request->get['product_id']) . '&product_id=' . $this->request->get['product_id']), 'canonical');
}else{
    $this->document->addLink($this->url->link('product/product', 'product_id=' . $this->request->get['product_id']), 'canonical');
}]]></add>
		</operation>
		<operation>
	<!-- CORRECTS PRODUCT PATH FOR ADDTHIS LINK -->
			<search position="replace"><![CDATA[
				$data['share'] = $this->url->link('product/product', 'product_id=' . (int)$this->request->get['product_id']);
			]]></search>
			<add><![CDATA[
				$data['share'] = $this->url->link('product/product', 'path=' . $this->model_catalog_product->getCategoryPath($this->request->get['product_id']) . '&product_id=' . (int)$this->request->get['product_id']);
			]]></add>
		</operation>
	</file>

	<file name="catalog/model/catalog/product.php">
		<operation>
			<search position="before" offset="1"><![CDATA[	public function getProduct($product_id) {]]></search>
			<add><![CDATA[
   public function getCategoryPath($product_id) {

	$query = $this->db->query("SELECT category_id AS catid FROM " . DB_PREFIX . "category WHERE category_id IN (SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' ) ORDER BY sort_order asc LIMIT 1");

	$babybooks = array(390, 33, 24, 55, 56, 249, 57);
	if($query->row['catid'] != 8 || in_array($product_id, $babybooks)){
		// Do another query to get/set minimum child category
		$query = $this->db->query("SELECT category_id AS catid FROM " . DB_PREFIX . "category WHERE category_id IN (SELECT MIN(category_id) FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' ) ORDER BY sort_order asc LIMIT 1");

		$cat = $this->db->query("SELECT model FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "' LIMIT 1");
		if(strstr($cat->row['model'], 'GS0')) {
			$query->row['catid'] = 16;
		    } elseif(strstr($cat->row['model'], 'GB0')) {
			$query->row['catid'] = 29;
		}
	} 

     
      if(array_key_exists('catid', $query->row)) {
         $path = array();
         $path[0] = $query->row['catid'];
         
         $query = $this->db->query("SELECT parent_id AS pid FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$path[0] . "'");

         $parent = $query->row['pid'];
         
         $p = 1;
         while($parent>0){
            $path[$p] = $parent;
            
            $query = $this->db->query("SELECT parent_id AS pid FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$parent . "'");
            $parent = $query->row['pid'];
            $p++;
         }
      
         $path = array_reverse($path);
         
         $fullpath = '';
         
         foreach($path as $val){
            $fullpath .= '_'.$val;
         }
      
         return ltrim($fullpath, '_');
      }else{
         return '0';
      }
   }]]></add>
		</operation>
	</file>

	<file name="catalog/controller/product/category.php">
		<operation>
			<search position="replace" offset="11"><![CDATA[// http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html]]></search>
			<add><![CDATA[if ($page == 1) {
			    $this->document->addLink($this->url->link('product/category', 'path=' . $this->request->get['path'], true), 'canonical');
			} elseif ($page == 2) {
			    $this->document->addLink($this->url->link('product/category', 'path=' . $this->request->get['path'], true), 'prev');
			} else {
			    $this->document->addLink($this->url->link('product/category', 'path=' . $this->request->get['path'] . '&page='. ($page - 1), true), 'prev');
			}

			if ($limit && ceil($product_total / $limit) > $page) {
			    $this->document->addLink($this->url->link('product/category', 'path=' . $this->request->get['path'] . '&page='. ($page + 1), true), 'next');
			}]]></add>
		</operation>
	</file>

</modification>

User avatar
New member

Posts

Joined
Sat May 27, 2017 12:12 am

Post by lukeketchum1 » Sat Jul 14, 2018 11:08 pm

Sorry to nudge a very old thread, but you see the extension above that KDNovelties wrote? I would like to use it. I guess I have to save that code to notepad as a specific type of extension and install it? This url issue is my final problem I need to fix before my site can become live, hence I am most excited to eliminate it :-D even if the above is not valid with OC3 if someone can recommend something which can enable me to do it would be good.

Active Member

Posts

Joined
Tue Jan 23, 2018 12:30 am

Post by IP_CAM » Sun Jul 15, 2018 11:00 am

Well, this VqMod would not even function in an OC v.1.5.6.x, by use of Code like this,
wich is sure not part of a default OC Source Code:

Code: Select all

<search position="replace" offset="11"><![CDATA[// http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html]]></search>
But some Extensions seem to exist, to also match OC v.3.x Versions as well, but unfortunately,
well-done latest Version Extension Code rarely comes for free at first, since Dev's have to bake
some bread again, once in a while, or then, they move away to something more profitable ! ::)
Good Luck!
Ernie
---
https://www.opencart.com/index.php?rout ... load_id=55
---

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by adibranch » Mon Oct 22, 2018 7:42 pm

The original OP is correct, and OC has indeed set this up in a very bad way. The canonical should reflect the URL structure the site is using, not its own version of it.

Whilst eventually this will sort itself out in the search engines, it is a VERY bad way of doing things, as you end up with a URL indexed in the search engines which is not linked to either internally or externally.

New member

Posts

Joined
Tue Jul 11, 2017 12:48 am
Who is online

Users browsing this forum: No registered users and 273 guests