Get page URL from current page, different store-view
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?
magento-1 multistore url store-view hreflang
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
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?
magento-1 multistore url store-view hreflang
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
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?
magento-1 multistore url store-view hreflang
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?
magento-1 multistore url store-view hreflang
magento-1 multistore url store-view hreflang
edited Apr 17 '18 at 16:20
sv3n
9,64062352
9,64062352
asked Jun 22 '16 at 16:07
Sander Koedood
3122414
3122414
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f122318%2fget-page-url-from-current-page-different-store-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Aug 23 '16 at 10:51
Vincent Hornikx
82128
82128
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Apr 17 '18 at 17:41
Rohan Hapani
5,83721662
5,83721662
answered Oct 5 '16 at 22:59
ffab
215
215
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f122318%2fget-page-url-from-current-page-different-store-view%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown