Magento 2 Get Product Url For Specific Store
I have multi stores enabled in my Magento instance. I am trying to get product url for each store as I need to index data in Elasticsearch.
What I have tried but not getting the result with store appended in url.
$productId = 2;
$storeId = 4;
$product = $this->productRepository->getById($productId, false, $storeId);
$product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
I expect result as
http://example.com/mystorecode/product1.html
Where as I get the result
http://example.com/product1.html
product-urls magento-2.1.8 elasticsearch
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 have multi stores enabled in my Magento instance. I am trying to get product url for each store as I need to index data in Elasticsearch.
What I have tried but not getting the result with store appended in url.
$productId = 2;
$storeId = 4;
$product = $this->productRepository->getById($productId, false, $storeId);
$product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
I expect result as
http://example.com/mystorecode/product1.html
Where as I get the result
http://example.com/product1.html
product-urls magento-2.1.8 elasticsearch
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.
Did you find any solution? I am also looking for the same query.
– Patrick W
Sep 24 '18 at 6:51
No. So I did a patchy solution which I have added as an answer. You can try it out. It worked out for me.
– Priyank
Sep 26 '18 at 11:04
Thanks for comment. I think below solution will work fine if you don't want to use object manager :$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB); $productUrl = $storeUrl .$productData->getUrlKey();
Here$productData
should be filtered with store id to bring store vise data.
– Patrick W
Sep 26 '18 at 11:57
add a comment |
I have multi stores enabled in my Magento instance. I am trying to get product url for each store as I need to index data in Elasticsearch.
What I have tried but not getting the result with store appended in url.
$productId = 2;
$storeId = 4;
$product = $this->productRepository->getById($productId, false, $storeId);
$product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
I expect result as
http://example.com/mystorecode/product1.html
Where as I get the result
http://example.com/product1.html
product-urls magento-2.1.8 elasticsearch
I have multi stores enabled in my Magento instance. I am trying to get product url for each store as I need to index data in Elasticsearch.
What I have tried but not getting the result with store appended in url.
$productId = 2;
$storeId = 4;
$product = $this->productRepository->getById($productId, false, $storeId);
$product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
I expect result as
http://example.com/mystorecode/product1.html
Where as I get the result
http://example.com/product1.html
product-urls magento-2.1.8 elasticsearch
product-urls magento-2.1.8 elasticsearch
edited Sep 4 '17 at 9:26
Priyank
asked Sep 1 '17 at 12:51
PriyankPriyank
5,12341949
5,12341949
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.
Did you find any solution? I am also looking for the same query.
– Patrick W
Sep 24 '18 at 6:51
No. So I did a patchy solution which I have added as an answer. You can try it out. It worked out for me.
– Priyank
Sep 26 '18 at 11:04
Thanks for comment. I think below solution will work fine if you don't want to use object manager :$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB); $productUrl = $storeUrl .$productData->getUrlKey();
Here$productData
should be filtered with store id to bring store vise data.
– Patrick W
Sep 26 '18 at 11:57
add a comment |
Did you find any solution? I am also looking for the same query.
– Patrick W
Sep 24 '18 at 6:51
No. So I did a patchy solution which I have added as an answer. You can try it out. It worked out for me.
– Priyank
Sep 26 '18 at 11:04
Thanks for comment. I think below solution will work fine if you don't want to use object manager :$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB); $productUrl = $storeUrl .$productData->getUrlKey();
Here$productData
should be filtered with store id to bring store vise data.
– Patrick W
Sep 26 '18 at 11:57
Did you find any solution? I am also looking for the same query.
– Patrick W
Sep 24 '18 at 6:51
Did you find any solution? I am also looking for the same query.
– Patrick W
Sep 24 '18 at 6:51
No. So I did a patchy solution which I have added as an answer. You can try it out. It worked out for me.
– Priyank
Sep 26 '18 at 11:04
No. So I did a patchy solution which I have added as an answer. You can try it out. It worked out for me.
– Priyank
Sep 26 '18 at 11:04
Thanks for comment. I think below solution will work fine if you don't want to use object manager :
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB); $productUrl = $storeUrl .$productData->getUrlKey();
Here $productData
should be filtered with store id to bring store vise data.– Patrick W
Sep 26 '18 at 11:57
Thanks for comment. I think below solution will work fine if you don't want to use object manager :
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB); $productUrl = $storeUrl .$productData->getUrlKey();
Here $productData
should be filtered with store id to bring store vise data.– Patrick W
Sep 26 '18 at 11:57
add a comment |
3 Answers
3
active
oldest
votes
From the Magento2 root, you can run below code inside any custom php file.
require './app/bootstrap.php';
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('admin');
$registry = $_objectManager->get('MagentoFrameworkRegistry');
$registry->register('isSecureArea', true);
$productId = 184294;
$storeId = 27;
$product = $_objectManager->create('MagentoCatalogApiProductRepositoryInterface')->getById($productId, false, $storeId);
echo $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL.
– Magecode
Feb 14 '18 at 11:44
add a comment |
From Admin > Stores > Configuration > Web > Url Options > Set "Add Store Code to Urls" to "Yes".
This option is enabled.
– Priyank
Sep 1 '17 at 12:59
I have just checked in my local system and your code working fine for me.
– Bhavin iFlair
Sep 1 '17 at 13:10
What is your magento version ? Are you trying to load product Object In Backend ?
– Priyank
Sep 1 '17 at 13:10
My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file.
– Bhavin iFlair
Sep 1 '17 at 13:13
Sorry dude not working for me. If you are using some other code paste in your answer.
– Priyank
Sep 1 '17 at 13:14
|
show 1 more comment
They patchy solution for my working
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB,$isSecure);
$productUrlKey = $product->getUrlKey();
$this->scopeConfig = $objectManager->create('MagentoFrameworkAppConfigScopeConfigInterface');
$productSuffix = $this->scopeConfig->getValue('catalog/seo/product_url_suffix', MagentoStoreModelScopeInterface::SCOPE_STORE);
$productUrl = $storeUrl . $productUrlKey . $productSuffix;
Note: Not recommend to use object manager. This shows my lazyness :)
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%2f191609%2fmagento-2-get-product-url-for-specific-store%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the Magento2 root, you can run below code inside any custom php file.
require './app/bootstrap.php';
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('admin');
$registry = $_objectManager->get('MagentoFrameworkRegistry');
$registry->register('isSecureArea', true);
$productId = 184294;
$storeId = 27;
$product = $_objectManager->create('MagentoCatalogApiProductRepositoryInterface')->getById($productId, false, $storeId);
echo $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL.
– Magecode
Feb 14 '18 at 11:44
add a comment |
From the Magento2 root, you can run below code inside any custom php file.
require './app/bootstrap.php';
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('admin');
$registry = $_objectManager->get('MagentoFrameworkRegistry');
$registry->register('isSecureArea', true);
$productId = 184294;
$storeId = 27;
$product = $_objectManager->create('MagentoCatalogApiProductRepositoryInterface')->getById($productId, false, $storeId);
echo $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL.
– Magecode
Feb 14 '18 at 11:44
add a comment |
From the Magento2 root, you can run below code inside any custom php file.
require './app/bootstrap.php';
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('admin');
$registry = $_objectManager->get('MagentoFrameworkRegistry');
$registry->register('isSecureArea', true);
$productId = 184294;
$storeId = 27;
$product = $_objectManager->create('MagentoCatalogApiProductRepositoryInterface')->getById($productId, false, $storeId);
echo $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
From the Magento2 root, you can run below code inside any custom php file.
require './app/bootstrap.php';
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('admin');
$registry = $_objectManager->get('MagentoFrameworkRegistry');
$registry->register('isSecureArea', true);
$productId = 184294;
$storeId = 27;
$product = $_objectManager->create('MagentoCatalogApiProductRepositoryInterface')->getById($productId, false, $storeId);
echo $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
answered Sep 1 '17 at 13:17
Bhavin iFlairBhavin iFlair
63617
63617
How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL.
– Magecode
Feb 14 '18 at 11:44
add a comment |
How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL.
– Magecode
Feb 14 '18 at 11:44
How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL.
– Magecode
Feb 14 '18 at 11:44
How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL.
– Magecode
Feb 14 '18 at 11:44
add a comment |
From Admin > Stores > Configuration > Web > Url Options > Set "Add Store Code to Urls" to "Yes".
This option is enabled.
– Priyank
Sep 1 '17 at 12:59
I have just checked in my local system and your code working fine for me.
– Bhavin iFlair
Sep 1 '17 at 13:10
What is your magento version ? Are you trying to load product Object In Backend ?
– Priyank
Sep 1 '17 at 13:10
My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file.
– Bhavin iFlair
Sep 1 '17 at 13:13
Sorry dude not working for me. If you are using some other code paste in your answer.
– Priyank
Sep 1 '17 at 13:14
|
show 1 more comment
From Admin > Stores > Configuration > Web > Url Options > Set "Add Store Code to Urls" to "Yes".
This option is enabled.
– Priyank
Sep 1 '17 at 12:59
I have just checked in my local system and your code working fine for me.
– Bhavin iFlair
Sep 1 '17 at 13:10
What is your magento version ? Are you trying to load product Object In Backend ?
– Priyank
Sep 1 '17 at 13:10
My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file.
– Bhavin iFlair
Sep 1 '17 at 13:13
Sorry dude not working for me. If you are using some other code paste in your answer.
– Priyank
Sep 1 '17 at 13:14
|
show 1 more comment
From Admin > Stores > Configuration > Web > Url Options > Set "Add Store Code to Urls" to "Yes".
From Admin > Stores > Configuration > Web > Url Options > Set "Add Store Code to Urls" to "Yes".
edited Sep 1 '17 at 13:42
answered Sep 1 '17 at 12:58
Bhavin iFlairBhavin iFlair
63617
63617
This option is enabled.
– Priyank
Sep 1 '17 at 12:59
I have just checked in my local system and your code working fine for me.
– Bhavin iFlair
Sep 1 '17 at 13:10
What is your magento version ? Are you trying to load product Object In Backend ?
– Priyank
Sep 1 '17 at 13:10
My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file.
– Bhavin iFlair
Sep 1 '17 at 13:13
Sorry dude not working for me. If you are using some other code paste in your answer.
– Priyank
Sep 1 '17 at 13:14
|
show 1 more comment
This option is enabled.
– Priyank
Sep 1 '17 at 12:59
I have just checked in my local system and your code working fine for me.
– Bhavin iFlair
Sep 1 '17 at 13:10
What is your magento version ? Are you trying to load product Object In Backend ?
– Priyank
Sep 1 '17 at 13:10
My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file.
– Bhavin iFlair
Sep 1 '17 at 13:13
Sorry dude not working for me. If you are using some other code paste in your answer.
– Priyank
Sep 1 '17 at 13:14
This option is enabled.
– Priyank
Sep 1 '17 at 12:59
This option is enabled.
– Priyank
Sep 1 '17 at 12:59
I have just checked in my local system and your code working fine for me.
– Bhavin iFlair
Sep 1 '17 at 13:10
I have just checked in my local system and your code working fine for me.
– Bhavin iFlair
Sep 1 '17 at 13:10
What is your magento version ? Are you trying to load product Object In Backend ?
– Priyank
Sep 1 '17 at 13:10
What is your magento version ? Are you trying to load product Object In Backend ?
– Priyank
Sep 1 '17 at 13:10
My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file.
– Bhavin iFlair
Sep 1 '17 at 13:13
My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file.
– Bhavin iFlair
Sep 1 '17 at 13:13
Sorry dude not working for me. If you are using some other code paste in your answer.
– Priyank
Sep 1 '17 at 13:14
Sorry dude not working for me. If you are using some other code paste in your answer.
– Priyank
Sep 1 '17 at 13:14
|
show 1 more comment
They patchy solution for my working
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB,$isSecure);
$productUrlKey = $product->getUrlKey();
$this->scopeConfig = $objectManager->create('MagentoFrameworkAppConfigScopeConfigInterface');
$productSuffix = $this->scopeConfig->getValue('catalog/seo/product_url_suffix', MagentoStoreModelScopeInterface::SCOPE_STORE);
$productUrl = $storeUrl . $productUrlKey . $productSuffix;
Note: Not recommend to use object manager. This shows my lazyness :)
add a comment |
They patchy solution for my working
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB,$isSecure);
$productUrlKey = $product->getUrlKey();
$this->scopeConfig = $objectManager->create('MagentoFrameworkAppConfigScopeConfigInterface');
$productSuffix = $this->scopeConfig->getValue('catalog/seo/product_url_suffix', MagentoStoreModelScopeInterface::SCOPE_STORE);
$productUrl = $storeUrl . $productUrlKey . $productSuffix;
Note: Not recommend to use object manager. This shows my lazyness :)
add a comment |
They patchy solution for my working
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB,$isSecure);
$productUrlKey = $product->getUrlKey();
$this->scopeConfig = $objectManager->create('MagentoFrameworkAppConfigScopeConfigInterface');
$productSuffix = $this->scopeConfig->getValue('catalog/seo/product_url_suffix', MagentoStoreModelScopeInterface::SCOPE_STORE);
$productUrl = $storeUrl . $productUrlKey . $productSuffix;
Note: Not recommend to use object manager. This shows my lazyness :)
They patchy solution for my working
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB,$isSecure);
$productUrlKey = $product->getUrlKey();
$this->scopeConfig = $objectManager->create('MagentoFrameworkAppConfigScopeConfigInterface');
$productSuffix = $this->scopeConfig->getValue('catalog/seo/product_url_suffix', MagentoStoreModelScopeInterface::SCOPE_STORE);
$productUrl = $storeUrl . $productUrlKey . $productSuffix;
Note: Not recommend to use object manager. This shows my lazyness :)
answered Sep 26 '18 at 11:03
PriyankPriyank
5,12341949
5,12341949
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%2f191609%2fmagento-2-get-product-url-for-specific-store%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
Did you find any solution? I am also looking for the same query.
– Patrick W
Sep 24 '18 at 6:51
No. So I did a patchy solution which I have added as an answer. You can try it out. It worked out for me.
– Priyank
Sep 26 '18 at 11:04
Thanks for comment. I think below solution will work fine if you don't want to use object manager :
$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_WEB); $productUrl = $storeUrl .$productData->getUrlKey();
Here$productData
should be filtered with store id to bring store vise data.– Patrick W
Sep 26 '18 at 11:57