Я кодирую, чтобы добавить динамический элементы hreflang к <head>
раздел каждой из наших веб-страниц в нашем магазине Magento.
Я написал несколько вспомогательных функций в Magento, которые я вызываю из нашего файла шаблона html / head.phtml. Я использую их, чтобы получить соответствующий URL для каждого вида магазина Magento. Они работают нормально, но я чувствую, что код может быть лучше.
Например:
$englishItalian
переменная.Все советы приветствуются
Шаблон html / head.phtml
<link rel="alternate" href="<?php echo Mage::helper('utility')->getEnglishDefaultUrl(); ?>" hreflang="en" />
<link rel="alternate" href="<?php echo Mage::helper('utility')->getEnglishUsUrl(); ?>" hreflang="en-us" />
<link rel="alternate" href="<?php echo Mage::helper('utility')->getItalianItalyUrl() ?>" hreflang="it-it" />
<link rel="alternate" href="<?php echo Mage::helper('utility')->getEnglishCanadaUrl(); ?>" hreflang="en-ca" />
<link rel="alternate" href="<?php echo Mage::helper('utility')->getFrenchCanadaUrl(); ?>" hreflang="fr-ca" />
Пространство имен / Utility / Helper / Data.php Вспомогательный класс
<?php
class Namespace_Utility_Helper_Data extends Mage_Core_Helper_Abstract
{
public function getEnglishDefaultUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId(); // get current store view ID
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 1,)); // get url for particular tore view
$href = $this->cleanUrl($href); // remove query strings from url
if ($curStoreId == 4) { // translate href depending on current store view ID
$href = $this->transItalianToEnglish($href);
return $href;
} elseif($curStoreId == 6){
$href = $this->transFrenchToEnglish($href);
return $href;
} else {
return $href;
}
}
public function getEnglishUsUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 2,));
$href = $this->cleanUrl($href);
if ($curStoreId == 4) {
$href = $this->transItalianToEnglish($href);
return $href;
} elseif($curStoreId == 6){
$href = $this->transFrenchToEnglish($href);
return $href;
} else {
return $href;
}
}
public function getItalianItalyUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 4,));
$href = $this->cleanUrl($href);
if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5 ) {
$href = $this->transEnglishToItalian($href);
return $href;
}elseif ($curStoreId == 6){
$href = $this->transFrenchToItalian($href);
return $href;
}else {
return $href;
}
}
public function getEnglishCanadaUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 5,));
$href = $this->cleanUrl($href);
if ($curStoreId == 4) {
$href = $this->transItalianToEnglish($href);
return $href;
} elseif($curStoreId == 6){
$href = $this->transFrenchToEnglish($href);
return $href;
} else {
return $href;
}
}
public function getFrenchCanadaUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 6,));
$href = $this->cleanUrl($href);
if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5 ) {
$href = $this->transEnglishToFrench($href);
return $href;
}elseif ($curStoreId == 4){
$href = $this->transItalianToFrench($href);
return $href;
}else {
return $href;
}
}
public function cleanUrl($url) // remove query string from href
{
$url = preg_replace('/\?.*/', '', $url);
return $url;
}
public function transEnglishToItalian($url)
{
$englishItalian = array( // use an associative array to store translations for urls
"products"=>"prodotti",
"science"=>"scienza",
"terms-and-conditions"=>"termini-e-condizioni",
"shipping"=>"spedizione",
);
foreach ($englishItalian as $key => $value) { // iterate over the array
if (strpos($url,$key) !== false) { // check if url has english translation word
$url = str_replace($key, $value, $url); // replace the english work with the Italian word
}
}
return $url;
}
public function transItalianToEnglish($url)
{
$englishItalian = array(
"products"=>"prodotti",
"science"=>"scienza",
"terms-and-conditions"=>"termini-e-condizioni",
"shipping"=>"spedizione",
);
foreach ($englishItalian as $key => $value) {
if (strpos($url,$value) !== false) {
$url = str_replace($value, $key, $url);
}
}
return $url;
}
public function transEnglishToFrench($url)
{
$englishFrench = array(
"products"=>"produits",
"shipping"=>"livraison",
);
foreach ($englishFrench as $key => $value) {
if (strpos($url,$key) !== false) {
$url = str_replace($key, $value, $url);
}
}
return $url;
}
public function transFrenchToEnglish($url)
{
$englishFrench = array(
"products"=>"produits",
"shipping"=>"livraison",
);
foreach ($englishFrench as $key => $value) {
if (strpos($url,$value) !== false) {
$url = str_replace($value, $key, $url);
}
}
return $url;
}
public function transItalianToFrench($url)
{
$italianFrench = array(
"prodotti"=>"produits",
"scienza"=>"science",
"termini-e-condizioni"=>"terms-and-conditions",
"spedizione"=>"livraison",
);
foreach ($italianFrench as $key => $value) {
if (strpos($url,$key) !== false) {
$url = str_replace($key, $value, $url);
}
}
return $url;
}
public function transFrenchToItalian($url)
{
$italianFrench = array(
"prodotti"=>"produits",
"scienza"=>"science",
"termini-e-condizioni"=>"terms-and-conditions",
"spedizione"=>"livraison",
);
foreach ($italianFrench as $key => $value) {
if (strpos($url,$value) !== false) {
$url = str_replace($value, $key, $url);
}
}
return $url;
}
}
Переместите массивы, которые вы повторяете, вверх.
Пространство имен / Utility / Helper / Data.php Вспомогательный класс
<?php
class Namespace_Utility_Helper_Data extends Mage_Core_Helper_Abstract
{
protected $englishItalian = array(
"products"=>"prodotti",
"science"=>"scienza",
"terms-and-conditions"=>"termini-e-condizioni",
"shipping"=>"spedizione",
);
protected $englishFrench = array(
"products"=>"produits",
"shipping"=>"livraison",
);
protected $italianFrench = array(
"prodotti"=>"produits",
"scienza"=>"science",
"termini-e-condizioni"=>"terms-and-conditions",
"spedizione"=>"livraison",
);
public function getEnglishDefaultUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId(); // get current store view ID
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 1,)); // get url for particular tore view
$href = $this->cleanUrl($href); // remove query strings from url
if ($curStoreId == 4) { // translate href depending on current store view ID
$href = $this->transItalianToEnglish($href);
return $href;
} elseif($curStoreId == 6){
$href = $this->transFrenchToEnglish($href);
return $href;
} else {
return $href;
}
}
public function getEnglishUsUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 2,));
$href = $this->cleanUrl($href);
if ($curStoreId == 4) {
$href = $this->transItalianToEnglish($href);
return $href;
} elseif($curStoreId == 6){
$href = $this->transFrenchToEnglish($href);
return $href;
} else {
return $href;
}
}
public function getItalianItalyUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 4,));
$href = $this->cleanUrl($href);
if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5 ) {
$href = $this->transEnglishToItalian($href);
return $href;
}elseif ($curStoreId == 6){
$href = $this->transFrenchToItalian($href);
return $href;
}else {
return $href;
}
}
public function getEnglishCanadaUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 5,));
$href = $this->cleanUrl($href);
if ($curStoreId == 4) {
$href = $this->transItalianToEnglish($href);
return $href;
} elseif($curStoreId == 6){
$href = $this->transFrenchToEnglish($href);
return $href;
} else {
return $href;
}
}
public function getFrenchCanadaUrl()
{
$curStoreId = Mage::app()->getStore()->getStoreId();
$href = Mage::getUrl('', array('_current' => true,'_use_rewrite' => true,'_store' => 6,));
$href = $this->cleanUrl($href);
if ($curStoreId == 1 || $curStoreId == 2 || $curStoreId == 5 ) {
$href = $this->transEnglishToFrench($href);
return $href;
}elseif ($curStoreId == 4){
$href = $this->transItalianToFrench($href);
return $href;
}else {
return $href;
}
}
public function cleanUrl($url) // remove query string from href
{
$url = preg_replace('/\?.*/', '', $url);
return $url;
}
public function transEnglishToItalian($url)
{
foreach ($this->englishItalian as $key => $value) { // iterate over the array
if (strpos($url,$key) !== false) { // check if url has english translation word
$url = str_replace($key, $value, $url); // replace the english work with the Italian word
}
}
return $url;
}
public function transItalianToEnglish($url)
{
foreach ($this->englishItalian as $key => $value) {
if (strpos($url,$value) !== false) {
$url = str_replace($value, $key, $url);
}
}
return $url;
}
public function transEnglishToFrench($url)
{
foreach ($this->englishFrench as $key => $value) {
if (strpos($url,$key) !== false) {
$url = str_replace($key, $value, $url);
}
}
return $url;
}
public function transFrenchToEnglish($url)
{
foreach ($this->englishFrench as $key => $value) {
if (strpos($url,$value) !== false) {
$url = str_replace($value, $key, $url);
}
}
return $url;
}
public function transItalianToFrench($url)
{
foreach ($this->italianFrench as $key => $value) {
if (strpos($url,$key) !== false) {
$url = str_replace($key, $value, $url);
}
}
return $url;
}
public function transFrenchToItalian($url)
{
foreach ($this->italianFrench as $key => $value) {
if (strpos($url,$value) !== false) {
$url = str_replace($value, $key, $url);
}
}
return $url;
}
}
Других решений пока нет …