cancel
Showing results for 
Search instead for 
Did you mean: 

Canonical Tags in Very old magento

Canonical Tags in Very old magento

Hi all,

Magento 1.4 onwards has supported canonical tags on category and product pages. I am however stranded with an old version of magento (1.3.2). Updating this isn't an option, and due to changes made by the company to the theme during development installing an old extension that enabled it isn't a safe option.

I do however have full access to all the files, and wondered if anybody could provide me with some code etc or a means of doing this. I have googled it extensivly but with no success.

Side note: I have managed to impliment it on CMS pages
Many Thanks

8 REPLIES 8

Re: Canonical Tags in Very old magento

add

        $headBlock = $this->getLayout()->getBlock('head');
		if ($headBlock)
		{
                //add canonical url with deault(1st) category info
                $params = array('_default_category'=>true);
$headBlock->addItem('link_rel', $product->getUrlModel()->getUrl($product, $params), 'rel="canonical"'); }

to rewrited Mage_Catalog_Block_Product_View::_prepareLayout

and in the same file, find/rewrite getCssJsHtml()

after

 

                case 'rss':
                    $lines[$if]['other'][] = sprintf($alternate, 'application/rss+xml'/*'text/xml' for IE?*/, $item['name'], $item['params']);
                    break;

insert

 

 

            case 'link_rel':
                $lines[$if]['other'][] = sprintf('<link%s href="%s" />', $item['params'], $item['name']);
                break;

 

 

you could also add prev and next tag with following steps

in rewrited Mage_Catalog_Block_Product_List create a helper function

 

    public function getPrevAndNextPageUrl()
    {
        $this->initToolbar();
        //init pager
        $this->getToolbarBlock()->getPagerHtml();
        $pager = $this->getToolbarBlock()->getChild('product_list_toolbar_pager');
        $urls = array('prev' => '', 'next' => '');
        if($pager){
            if(!$pager->isFirstPage()){
                $urls['prev'] = $pager->getPreviousPageUrl();
            }
            if(!$pager->isLastPage()){
                $urls['next'] = $pager->getNextPageUrl();
            }
        }
        return $urls;
    }

then in rewrited Mage_Page_Block_Html_Head

 

 

    public function addCategoryLink($blockName)
    {
        if(($block = $this->getLayout()->getBlock($blockName)) && ($urls = $block->getPrevAndNextPageUrl())){
            if(isset($urls['prev']) && $urls['prev']){
$this->addItem('link_rel', $urls['prev'], 'rel="prev"') } if(isset($urls['next']) && $urls['next']){ $this->addItem('link_rel', $urls['next'], 'rel="next"'); } } return $this; }

finally added the links in layout xml

 

 

    <catalog_category_default>
        <reference name="head">
            <action method="addCategoryLink"><blockName>product_list</blockName></action>
        </reference>
    </catalog_category_default>

    <catalog_category_layered>
        <reference name="head">
            <action method="addCategoryLink"><blockName>product_list</blockName></action>
        </reference>
    </catalog_category_layered>

 

 

 

Re: Canonical Tags in Very old magento

Thanks very much for the reply KuafuSoft, I apreciate your time.

Im a little confused as the exactly where I should be adding these code snippets plz could you elaborate further?

to rewrited Mage_Catalog_Block_Product_View::_prepareLayout

is this meant to be a file path or a bit of code within a file?

Could you possible give me the full path from the root domain of any file I need to edit and if possible the exact place within the file I should be inserting the code?

Re: Canonical Tags in Very old magento

in app/code/core/Mage/Catalog/Block/Product/View.php, the very first method _prepareLayout()

insert the snippt before return parent::_preparelayout();

 

if you know how to rewrite core class, insert in your own class, modifing core files is bad practice.

Re: Canonical Tags in Very old magento

    protected function _prepareLayout()

    {

        $this->getLayout()->createBlock('catalog/breadcrumbs');

        

        if ($headBlock = $this->getLayout()->getBlock('head')) {      

        		 	

            if ($title = $this->getProduct()->getMetaTitle()) {            	            	

                $headBlock->setTitle($title);

            }

						$headBlock->setTitle($this->getProduct()->getName());

            if ($keyword = $this->getProduct()->getMetaKeyword()) {

                $headBlock->setKeywords($keyword);

            } elseif( $currentCategory = Mage::registry('current_category') ) {

                $headBlock->setKeywords($this->getProduct()->getName());

            }



            if ($description = $this->getProduct()->getMetaDescription()) {

                $headBlock->setDescription( ($description) );

            } else {

                $headBlock->setDescription( $this->getProduct()->getDescription() );

            }

        }

        $headBlock = $this->getLayout()->getBlock('head');
            if ($headBlock)
		{
                //add canonical url with deault(1st) category info
                $params = array('_default_category'=>true);
                $headBlock->addLinkRel('canonical', $product->getUrlModel()->getUrl($product, $params));
                }

        return parent::_prepareLayout();

    }

Ive added your code snippet into the file and location you specified as of the code above.

What other steps do I need to do to make the tag actually appear? As nothing has yet by just doing this.

Thanks for your time!


Re: Canonical Tags in Very old magento

I modified previouse post, please change code and try again

Re: Canonical Tags in Very old magento

Just tried the new code, same problem as the previous one, no tag is being added Smiley Sad

Any other ideas?

Thanks again

Re: Canonical Tags in Very old magento

sorry I don't have 1.3.2 so I can't  test it myself.

You can turn on logging see if there are any error messages, debug with var_dump see if canonical tag is actually inserted to $lines array and outputed in $html in the end of getCssJsHtml

Re: Canonical Tags in Very old magento

OK no problem thankyou for your efforts.

Ive only just started learning php etc so i will keep learning and save the info in this post and try to get it working when I know how to do more stuff.

Appreciate your time. thanks for trying!