6

I'm using different store-views to create different language versions of a shop. Now I'm trying to add links to the other pages. However, I can not find a way to get the actual URL from the other store, if the url of that page / product / category is changed for that store (in this case, translated). I've got the following code at this moment:

<?php
// Get the current website
$website = Mage::app()->getWebsite();
// Only get languages/stores from the current website
$stores = $website->getStores();
// loop through the stores
foreach ($stores as $_lang):
    // If it's not the current store, create an alternative for it
    if( $_lang->getId() != Mage::app()->getStore()->getStoreId() ){ 
        // Get the language code for that store view
        $lang_code = substr(Mage::getStoreConfig('general/locale/code', $_lang->getId()),0,2);
        // Create a link, using getCurrentUrl for the set store ?>
        <link rel="alternate" hreflang="<?php echo $lang_code; ?>" href="<?php echo $_lang->getCurrentUrl(); ?>" />
    <?php } ?>
<?php endforeach; ?>

However, this returns the same URL, with a ?___store=fr&___from_store=nl at the end. It should however give the URL from the different store, which has a different url key. I've also tried the following:

Mage::getUrl('', array(
    '_current' => true,
    '_use_rewrite' => true,
    '_secure' => true,
    '_store' => $_lang->getId(),
    '_store_to_url' => true
));

But this gave the exact same results.

Is there a way to retrieve the right URL without having to use a redirect?

0

When accessing the alternate link which your code generates Magento should rewrite and redirect to the correct URL. You can take a look at Mage_Core_Model_Url_Rewrite_Request::_rewriteDb to see this rewrite process.

Basically it maps the request path from the ___from_store scope to the ___store scope.

You could mimic this rewrite process but I would advise against it. You would be performing a lot of database actions without knowing if the result will be used or not, and seeing that the 'ugly' URL is eventually redirect to the correct 'pretty' URL anyway, it doesn't seem to matter, especially if you're outputting these URL's in a non-visible way.

Regards, Vincent

0

You have to create a Module/Helper like this one:

app/code/local/Your/Rewrites/Helper/Data.php

class Your_Rewrites_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();
    }

    public function rewrittenCategoryUrl($categoryId, $storeId)
    {
        $coreUrl = Mage::getModel('core/url_rewrite');
        $idPath = sprintf('category/%d', $categoryId);
        $coreUrl->setStoreId($storeId);
        $coreUrl->loadByIdPath($idPath);

        return $coreUrl->getRequestPath();
    }
}

and

etc/config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Your_Rewrites>
            <version>1.0</version>
        </Your_Rewrites>
    </modules>
    <global>
        <helpers>
          <your_rewrites>
              <class>Your_Rewrites_Helper</class>
          </your_rewrites>
        </helpers>
    </global>
</config>

and call it from your languages.phtml which, I suppose you working with or in any other phtml with theses kind of lines

$helper     = Mage::helper('your_rewrites');
$prod       = Mage::registry('current_product');
$categ      = Mage::registry('current_category');
$categId    = $categ ? $categ->getId() : null;

foreach ($this->getStores() as $_lang) {
    if ($prod) {
        $link[$_lang->getId()] = $_lang->getBaseUrl() . $helper->rewrittenProductUrl($prod->getId(), $categId, $_lang->getId());
    } elseif ($categ) {
        $link[$_lang->getId()] = $_lang->getBaseUrl() . $helper->rewrittenCategoryUrl($categId, $_lang->getId());
    } else {
        $link[$_lang->getId()] = $_lang->getCurrentUrl(false);
    }
}

This will basically get according links from other stores, for products, categories, and fallback if not product/catagory type & put them into a links array. Of course you have to change the end of that file to display the link as you wish to.

Read more : This code is an adaptation of this Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.