Trouble getting product URLs
After looking at various methods, the following code is almost getting what I want:
$product = Mage::getModel('catalog/product')->load($product_id);
$site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
$product_url = $site_url.$product->getUrlPath($category);
}
This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.
However this produces a 404.
570 is not the ID that corresponds to this product so I'm wondering where it's coming from.
url
add a comment |
After looking at various methods, the following code is almost getting what I want:
$product = Mage::getModel('catalog/product')->load($product_id);
$site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
$product_url = $site_url.$product->getUrlPath($category);
}
This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.
However this produces a 404.
570 is not the ID that corresponds to this product so I'm wondering where it's coming from.
url
add a comment |
After looking at various methods, the following code is almost getting what I want:
$product = Mage::getModel('catalog/product')->load($product_id);
$site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
$product_url = $site_url.$product->getUrlPath($category);
}
This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.
However this produces a 404.
570 is not the ID that corresponds to this product so I'm wondering where it's coming from.
url
After looking at various methods, the following code is almost getting what I want:
$product = Mage::getModel('catalog/product')->load($product_id);
$site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
$product_url = $site_url.$product->getUrlPath($category);
}
This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.
However this produces a 404.
570 is not the ID that corresponds to this product so I'm wondering where it's coming from.
url
url
edited 1 hour ago
Teja Bhagavan Kollepara
3,00641949
3,00641949
asked Mar 24 '17 at 17:42
mikejwmikejw
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Instead of using $product_url = $site_url.$product->getUrlPath($category);
those.
You use below code
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
if($category->getId()){
$product->setCategory($category);
}
}
$product_url = $product->getProductUrl();
Or via ProductCollection with addUrlRewrite() function:
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = $product->getProductUrl();
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter($product->getId());
if($collection->getSize() >= 1):
/* Add addUrlRewrite for add category url with product */
$collection->addUrlRewrite((int)$cats[0]);
$ProductWithCategory = $collection->getFirstItem();
$product_url = $ProductWithCategory->getProductUrl();
endif;
}
Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.
– mikejw
Mar 27 '17 at 9:53
Please do indexing from shell check and check the result
– Amit Bera♦
Mar 27 '17 at 9:57
Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.
– mikejw
Mar 27 '17 at 10:11
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%2f166106%2ftrouble-getting-product-urls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of using $product_url = $site_url.$product->getUrlPath($category);
those.
You use below code
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
if($category->getId()){
$product->setCategory($category);
}
}
$product_url = $product->getProductUrl();
Or via ProductCollection with addUrlRewrite() function:
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = $product->getProductUrl();
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter($product->getId());
if($collection->getSize() >= 1):
/* Add addUrlRewrite for add category url with product */
$collection->addUrlRewrite((int)$cats[0]);
$ProductWithCategory = $collection->getFirstItem();
$product_url = $ProductWithCategory->getProductUrl();
endif;
}
Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.
– mikejw
Mar 27 '17 at 9:53
Please do indexing from shell check and check the result
– Amit Bera♦
Mar 27 '17 at 9:57
Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.
– mikejw
Mar 27 '17 at 10:11
add a comment |
Instead of using $product_url = $site_url.$product->getUrlPath($category);
those.
You use below code
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
if($category->getId()){
$product->setCategory($category);
}
}
$product_url = $product->getProductUrl();
Or via ProductCollection with addUrlRewrite() function:
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = $product->getProductUrl();
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter($product->getId());
if($collection->getSize() >= 1):
/* Add addUrlRewrite for add category url with product */
$collection->addUrlRewrite((int)$cats[0]);
$ProductWithCategory = $collection->getFirstItem();
$product_url = $ProductWithCategory->getProductUrl();
endif;
}
Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.
– mikejw
Mar 27 '17 at 9:53
Please do indexing from shell check and check the result
– Amit Bera♦
Mar 27 '17 at 9:57
Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.
– mikejw
Mar 27 '17 at 10:11
add a comment |
Instead of using $product_url = $site_url.$product->getUrlPath($category);
those.
You use below code
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
if($category->getId()){
$product->setCategory($category);
}
}
$product_url = $product->getProductUrl();
Or via ProductCollection with addUrlRewrite() function:
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = $product->getProductUrl();
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter($product->getId());
if($collection->getSize() >= 1):
/* Add addUrlRewrite for add category url with product */
$collection->addUrlRewrite((int)$cats[0]);
$ProductWithCategory = $collection->getFirstItem();
$product_url = $ProductWithCategory->getProductUrl();
endif;
}
Instead of using $product_url = $site_url.$product->getUrlPath($category);
those.
You use below code
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
if($category->getId()){
$product->setCategory($category);
}
}
$product_url = $product->getProductUrl();
Or via ProductCollection with addUrlRewrite() function:
$product = Mage::getModel('catalog/product')->load($product_id);
$product_url = $product->getProductUrl();
$cats = $product->getCategoryIds();
if (sizeof($cats)) {
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addIdFilter($product->getId());
if($collection->getSize() >= 1):
/* Add addUrlRewrite for add category url with product */
$collection->addUrlRewrite((int)$cats[0]);
$ProductWithCategory = $collection->getFirstItem();
$product_url = $ProductWithCategory->getProductUrl();
endif;
}
edited Mar 24 '17 at 18:15
answered Mar 24 '17 at 18:06
Amit Bera♦Amit Bera
59.3k1575177
59.3k1575177
Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.
– mikejw
Mar 27 '17 at 9:53
Please do indexing from shell check and check the result
– Amit Bera♦
Mar 27 '17 at 9:57
Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.
– mikejw
Mar 27 '17 at 10:11
add a comment |
Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.
– mikejw
Mar 27 '17 at 9:53
Please do indexing from shell check and check the result
– Amit Bera♦
Mar 27 '17 at 9:57
Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.
– mikejw
Mar 27 '17 at 10:11
Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.
– mikejw
Mar 27 '17 at 9:53
Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.
– mikejw
Mar 27 '17 at 9:53
Please do indexing from shell check and check the result
– Amit Bera♦
Mar 27 '17 at 9:57
Please do indexing from shell check and check the result
– Amit Bera♦
Mar 27 '17 at 9:57
Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.
– mikejw
Mar 27 '17 at 10:11
Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.
– mikejw
Mar 27 '17 at 10:11
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.
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%2f166106%2ftrouble-getting-product-urls%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