Canonical URL

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108206

I am in the process of setting up a new store for a client and would like to make use of the Canonical URL feature in Hikashop.

I have a few categories and have set the URL for these. So /test/shop/books-magazines/product/listing has a Canonical URL of /books-and-magazines and then test/shop/books-magazines/product/52-english-language-mens-magazines-12-pack becomes /mens-magazine-12-pack-english-language.

I then tried the same proccess for a product that has options but no canonical url is created. test/shop/male-female-clothing/product/18-sweatshirt should then be /unisex-sweatshirt. Am I missing something?

Thanks

Dave B)

Please Log in or Create an account to join the conversation.

  • Posts: 82759
  • Thank you received: 13346
  • MODERATOR
11 years 4 months ago #108224

I don't understand your question since HikaShop doesn't create any canonical URL. It is your job to enter them into each product/category. So I can't answer because I don't see the problem. Could you clarify what you mean ?

Please Log in or Create an account to join the conversation.

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108290

Nicolas

apologies if it wasn't very clear.

I have entered these manually but on a product that has otpions such as the sweatshirt with a size and colour the canonical url is not showing?

Dave B)

Please Log in or Create an account to join the conversation.

  • Posts: 82759
  • Thank you received: 13346
  • MODERATOR
11 years 4 months ago #108385

Hi,

I don't think that it comes from product options. It's probably because of product characteristics.

Try to change the code:

if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){
	$canonicalUrl = hikashop_cleanURL($this->element->product_canonical);

	$doc =& JFactory::getDocument();
	$doc->addCustomTag( '<link rel="canonical" href="'.$canonicalUrl.'" />' );
}
to:
$canonical=false;
if(!empty($this->element->main->product_canonical)){
	$canonical = $this->element->main->product_canonical;
}elseif(!empty($this->element->product_canonical)){
	$canonical = $this->element->product_canonical;
}
if($canonical){
	$doc = JFactory::getDocument();
	$doc->addCustomTag( '<link rel="canonical" href="'.hikashop_cleanURL($canonical).'" />' );
}
in the file "show" of the view "product" for your frontend template via the menu Display->Views and that should help.

Please Log in or Create an account to join the conversation.

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108412

Nicolas

I have tried this and get an error? The code I have on that page is

<?php
if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){

  $parsedCanonical = parse_url($this->element->product_canonical);
  $parsedCurrent = parse_url(JURI::base());

  if(!isset($parsedCanonical['query']))
    $endUrl = $parsedCanonical['path'];
  else
    $endUrl = $parsedCanonical['path'].'?'.$parsedCanonical['query'];

  $canonicalUrl = $parsedCurrent['scheme'].'://'.$parsedCurrent['host'].$endUrl;

  $doc =& JFactory::getDocument();
  $doc->addCustomTag( '<link rel="canonical" href="'.$canonicalUrl.'" />' );
}
?>

Dave B)

Please Log in or Create an account to join the conversation.

  • Posts: 2334
  • Thank you received: 403
11 years 4 months ago #108435

Hi there,

Indeed, the function cleanUrl just has been added. Try to add this before $parsedCanonical = parse_url($this->element->product_canonical); :

if(!function_exists(hikashop_cleanURL())){
function hikashop_cleanURL($url, $forceInternURL=false){
	$parsedURL = parse_url($url);
	$parsedCurrent = parse_url(JURI::base());
	if($forceInternURL==false){
		if(isset($parsedURL['scheme'])){
			return $url;
		}
	}
	if($parsedURL['path'][0]!='/' && $parsedURL['path'][0]!='h' && $parsedURL['path'][0]!='w'){
		$parsedURL['path']='/'.$parsedURL['path'];
	}

	if(!isset($parsedURL['query']))
		$endUrl = $parsedURL['path'];
	else
		$endUrl = $parsedURL['path'].'?'.$parsedURL['query'];

	$cleanUrl = $parsedCurrent['scheme'].'://'.$parsedCurrent['host'].$endUrl;
	return $cleanUrl;
}
}

Please Log in or Create an account to join the conversation.

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108440

Eliot

thanks for the reply. The default code for views product show is

<?php
if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){
 
  $parsedCanonical = parse_url($this->element->product_canonical);
  $parsedCurrent = parse_url(JURI::base());
 
  if(!isset($parsedCanonical['query']))
    $endUrl = $parsedCanonical['path'];
  else
    $endUrl = $parsedCanonical['path'].'?'.$parsedCanonical['query'];
 
  $canonicalUrl = $parsedCurrent['scheme'].'://'.$parsedCurrent['host'].$endUrl;
 
  $doc =& JFactory::getDocument();
  $doc->addCustomTag( '<link rel="canonical" href="'.$canonicalUrl.'" />' );
}
?>

Nicolas advised me to change it to
$canonical=false;
if(!empty($this->element->main->product_canonical)){
	$canonical = $this->element->main->product_canonical;
}elseif(!empty($this->element->product_canonical)){
	$canonical = $this->element->product_canonical;
}
if($canonical){
	$doc = JFactory::getDocument();
	$doc->addCustomTag( '<link rel="canonical" href="'.hikashop_cleanURL($canonical).'" />' );
}

I then get an error

Call to undefined function hikashop_cleanURL() in /test/templates/shape5_vertex/html/com_hikashop/product/show.php on line 19

Dave B)

Please Log in or Create an account to join the conversation.

  • Posts: 2334
  • Thank you received: 403
11 years 4 months ago #108469

You should read my message again ;)
Try to add the code I gave you after the first if(), it should work :)

Please Log in or Create an account to join the conversation.

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108481

Hi

The first part of the code that Nicolas gave me is not in the view product show file that I have.

if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){
	$canonicalUrl = hikashop_cleanURL($this->element->product_canonical);

My first lines are before applying changes are.
if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){

  $parsedCanonical = parse_url($this->element->product_canonical);
  $parsedCurrent = parse_url(JURI::base());

Thanks

Dave B)

Last edit: 11 years 4 months ago by davec.

Please Log in or Create an account to join the conversation.

  • Posts: 2334
  • Thank you received: 403
11 years 4 months ago #108629

As I told you, just add the whole block of code I gave you between the first line:

if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){
and the second line:

$parsedCanonical = parse_url($this->element->product_canonical);

It should work fine

Please Log in or Create an account to join the conversation.

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108648

I have tried this and it presents a blank page.

My first lines of code are

if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){

  $parsedCanonical = parse_url($this->element->product_canonical);
  $parsedCurrent = parse_url(JURI::base());

I placed the code within the space and it is throwing up a blank page.

Dave B)

Please Log in or Create an account to join the conversation.

  • Posts: 2334
  • Thank you received: 403
11 years 4 months ago #108701

My bad, the first line of the code I gave you should be:

if(!function_exists('hikashop_cleanURL')){

Please Log in or Create an account to join the conversation.

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108776

Thanks Eliot

I am afraid though that I am still not being given a canonical url for the products with characteristics. Below is the code after the customisation was made.

if(isset($this->element->product_canonical) && !empty($this->element->product_canonical)){
if(!function_exists('hikashop_cleanURL')){
function hikashop_cleanURL($url, $forceInternURL=false){
  $parsedURL = parse_url($url);
  $parsedCurrent = parse_url(JURI::base());
  if($forceInternURL==false){
    if(isset($parsedURL['scheme'])){
      return $url;
    }
  }
  if($parsedURL['path'][0]!='/' && $parsedURL['path'][0]!='h' && $parsedURL['path'][0]!='w'){
    $parsedURL['path']='/'.$parsedURL['path'];
  }
 
  if(!isset($parsedURL['query']))
    $endUrl = $parsedURL['path'];
  else
    $endUrl = $parsedURL['path'].'?'.$parsedURL['query'];
 
  $cleanUrl = $parsedCurrent['scheme'].'://'.$parsedCurrent['host'].$endUrl;
  return $cleanUrl;
}
}
  $parsedCanonical = parse_url($this->element->product_canonical);
  $parsedCurrent = parse_url(JURI::base());

  if(!isset($parsedCanonical['query']))
    $endUrl = $parsedCanonical['path'];
  else
    $endUrl = $parsedCanonical['path'].'?'.$parsedCanonical['query'];

  $canonicalUrl = $parsedCurrent['scheme'].'://'.$parsedCurrent['host'].$endUrl;

  $doc =& JFactory::getDocument();
  $doc->addCustomTag( '<link rel="canonical" href="'.$canonicalUrl.'" />' );
}

Thanks

Dave B)

Please Log in or Create an account to join the conversation.

  • Posts: 82759
  • Thank you received: 13346
  • MODERATOR
11 years 4 months ago #108788

You should not do like that but like that:

if(!function_exists('hikashop_cleanURL')){
function hikashop_cleanURL($url, $forceInternURL=false){
  $parsedURL = parse_url($url);
  $parsedCurrent = parse_url(JURI::base());
  if($forceInternURL==false){
    if(isset($parsedURL['scheme'])){
      return $url;
    }
  }
  if($parsedURL['path'][0]!='/' && $parsedURL['path'][0]!='h' && $parsedURL['path'][0]!='w'){
    $parsedURL['path']='/'.$parsedURL['path'];
  }
 
  if(!isset($parsedURL['query']))
    $endUrl = $parsedURL['path'];
  else
    $endUrl = $parsedURL['path'].'?'.$parsedURL['query'];
 
  $cleanUrl = $parsedCurrent['scheme'].'://'.$parsedCurrent['host'].$endUrl;
  return $cleanUrl;
}
}
$canonical=false;
if(!empty($this->element->main->product_canonical)){
	$canonical = $this->element->main->product_canonical;
}elseif(!empty($this->element->product_canonical)){
	$canonical = $this->element->product_canonical;
}
if($canonical){
	$doc = JFactory::getDocument();
	$doc->addCustomTag( '<link rel="canonical" href="'.hikashop_cleanURL($canonical).'" />' );
}

The following user(s) said Thank You: davec

Please Log in or Create an account to join the conversation.

  • Posts: 440
  • Thank you received: 20
  • Hikashop Essential
11 years 4 months ago #108821

Nicolas & Eliot

thanks very much for your help. All now working as expeced

Dave B)

Please Log in or Create an account to join the conversation.

Time to create page: 0.088 seconds
Powered by Kunena Forum