Getting link rel=”prev/next” links in Magento category pages

Google likes this stuff, so we should utilise tags in the.I could probably put this code in a better place, but for now, I went into my head view file located at app/design/frontend/themename/default/template/page/html/head.phtml

if ($this->getAction()->getFullActionName() == 'catalog_category_view') // Category Page
{
    $category = Mage::registry('current_category');
    
    $collection = $category->getProductCollection()
                        ->addAttributeToFilter('status', 1)
                        ->addAttributeToFilter('visibility', array(
                            'in' => array(
                                Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, 
                                Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                            )
                        );
    /** @var @var Mage_Page_Block_Html_Pager $tool */
    $tool = $this->getLayout()
                 ->createBlock('page/html_pager')
                 ->setLimit($this->getLayout()
                 ->createBlock('catalog/product_list_toolbar')
                 ->getLimit())
                 ->setCollection($collection);

    $prev = false;
    $next = false;
    if ($tool->getCollection()->getSelectCountSql())
    {
        if ($tool->getLastPageNum() > 1)
        {
            if (!$tool->isFirstPage())
            {
                $prev = true;
                if ($tool->getCurrentPage() == 2)
                {
                    $url = explode('?', $tool->getPreviousPageUrl());
                    $prev_url = @$url[0];
                }
                else
                {
                    $prev_url = $tool->getPreviousPageUrl();
                }
            }
            if (!$tool->isLastPage()) {
                $next = true;
                $next_url = $tool->getNextPageUrl();
            }
        }
    }
    echo ($prev) ?'<link rel="prev" href="' . $prev_url . '" />' : null;
    echo ($next) ? '<link rel="next" href="' . $next_url . '" />' : null;
}

If you DON’T have category pages with duplicate content that can be accessed from multiple urls, you can now turn off the canonical tag in the SEO settings. Log into the admin panel, and select System > Configuration > Catalogue  > Search Engine Optimisations. Then you can set Use Canonical Link Meta Tag For Categories to No.

Again, leave it yes if you have duplicate content, ie /crystal-wine-glasses has the same (but less) content than /wine-glasses.

Job done! Google will like that! 😀

Google QR Codes and Zend_PDF

Now that we can generate QR codes using the Google Chart API, and we have a controller action that serves it up for us with the correct .PNG header (see my previous post), we need to get it where it’s of any use – on paper!

So following on from my last post about Zend_Pdf, we can now do the following to get it in our PDF:

//draw qr code
 $target = 'http://example.co.uk/download/qr?target=http://www.example.co.uk/properties/view/id/'.$prop->getID();
 $img = file_get_contents($target); 
 file_put_contents(APPLICATION_PATH.'/uploads/tmp/'.$prop->getID().'-qr.png',$img);
 $image = Zend_Pdf_Image::imageWithPath(APPLICATION_PATH.'/uploads/tmp/'.$prop->getID().'-qr.png');
 $page->drawImage($image, 13, 13, 94,94);
 unlink(APPLICATION_PATH.'/uploads/tmp/'.$prop->getPid().'-qr.png');

The first line of code is the url of our download controllers QR action.  file_get_contents stores the image in memory, which we then save into a temp folder using file_put_contents. These commands are the equivalent of a lot of fopen etc, so use them if you don’t already!  This example uses my property object to make a dynamic filename, but this isn’t necessary, but I don’t need to tell you that!

We then use Zend_Pdf to load our png, and when drawing an image, the numbers in the argument are x1, y1, x2, y2 being bottom left and top right corners respectively. Finally, ditch the temp file with the unlink command. Have Fun!