随着电子商务的发展,商品销往全球比以前容易多了。互联网没有界限,任何人都可以在舒适的家里访问你的商店。对于多店铺的需求也是日益明显。也许你希望让你的客户访问你的网站时看到的是自己的语言。

当你设置你的多店铺时,Google有些指导和建议。这样能让你的网站索引更正确,排名更高。其中一条建议就是:如果你有翻译成其它语言的页面,使用rel="alternate"的链接。

我们使用的语法看起来就像这样:

<link rel="alternate" hreflang="es" href="http://es.example.com/" />

如果把这个语法应用到Magento里,那看起来就像这样:



现在我们就有了思路。让我们以注册我们的模块开始。

app/etc/modules/Alwayly_Alternate.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Alwayly_Alternate>
            <active>true</active>
            <codePool>local</codePool>
        </Alwayly_Alternate>
   </modules>
</config>

让我们谈下模块的设置。我们需要做的就是把alternate链接放到每个页面的头部。一种方法就是在head.phtm文件里创建逻辑请求。更好的方式是,使用观测者监听一些时间,然后使用Magento中已经存在的addLinkRel()方法加入到head里。

在这个例子里,我们将使用controller_action_layout_generate_blocks_after事件。也许这儿还有些更合适的事件,但这一次就用它了。

app/code/local/Alwayly/Alternate/etc/config.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Alwayly_Alternate>
            <version>1.0.0</version>
        </Alwayly_Alternate>
    </modules>
    <global>
        <helpers>
            <alwayly_alternate>
                <class>Alwayly_Alternate_Helper<class>
            </alwayly_alternate>
        </helpers>
    </global>
    <frontend>
        <events>
            <controller_action_layout_generate_blocks_after>
                <observers>
                    <alwayly_alternate>
                        <type>singleton</type>
                        <class>Alwayly_Alternate_Model_Observer</class>
                        <method>alternateLinks</method>
                    </alwayly_alternate>
                </observers>
            </controller_action_layout_generate_blocks_after>
        </events>
    </frontend>
</config>

我们只是定义了观测者的名字和方法,现在来创建它。

app/code/local/Alwayly/Alternate/Model/Observer.php


class Alwayly_Alternate_Model_Observer
{
 public function alternateLinks()
 {
   $headBlock = Mage::app()->getLayout()->getBlock('head');
 
   $stores = Mage::app()->getStores();
   $prod = Mage::registry('current_product');
   $categ = Mage::registry('current_category');
 
   if($headblock)
   {
     foreach ($stores as $store)
     {
       if($prod){
         $categ ? $categId=$categ->getId() : $categId = null;
         $url = $store->getBaseUrl() . Mage::helper('alwayly_alternate')->rewrittenProductUrl($prod->getId(), $categId, $store->getId());
       }else{
         $store->getCurrentUrl();
       }
       $storeCode = substr(Mage::getStoreConfig('general/locale/code', $store->getId()),0,2);
       $headBlock->addLinkRel('alternate"' . ' hreflang="' . $storeCode, $url);
     }
   }
   return $this;
 }
}

如你所见,我们循环所有的商店。当一个产品被设置,我们检查它的URL是否被重写。这是我们的助手要做的。如果一个产品没被设置,用getCurrentUrl()方法,我们能获取不同商店中的当前URL。

例如我们现在的URL是 www.example.com/shirts.html,在德语版的商店,它会是www.example.com/shirts.html?___store=german或者是www.example.com/de/shirts.html,这取决于你的Magento设置。

下一步,我们将获取区域代码的前一到两个字母。如果你有两个英文商店,一个为英国,一个为美国,那就完美了。在得到我们所要的信息后,我们将调用head块,使用addLinRel()方法将链接加到head。

现在,创建你的助手。


<?php
class Alwayly_Alternate_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function rewrittenProductUrl($productId, $categoryId, $storeId)
    {
        $coreUrl = Mage::getModel('core/url_rewrite');
        $idPath = sprintf('product/%d', $productId);
        if ($categoryId) {
            $idPath = sprintf('%s/%d', $idPath, $categoryId);
        }
        $coreUrl->setStoreId($storeId);
        $coreUrl->loadByIdPath($idPath);
        return $coreUrl->getRequestPath();
    }
}

360magento提供专业的基于magento系统的电商网站开发服务,如有需求或相关咨询,请与我们联系