Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Relate, Up-sell and Cross-sell Products Use wrong URL
 
jimmypalmer
Sr. Member
 
Total Posts:  79
Joined:  2008-05-04
 

I’ve seen a lot of talk on the whole duplicated content issue with having two (or more) different links pointing to the same product page, but nothing that really solves the problem.  Maybe I’m not getting it and maybe there isn’t a great solution.

The Problem:
All my products have one category each and I want the product’s category name to be in every product url (http://www.mydomain.com/category/product.html)

‘Use categories path for product URLs’ is set to “Yes”.

When searching through the products in the category pages the category name shows up in each of the product url’s, HOWEVER, when adding related products, up sell products or cross sell products the url of that product does NOT include the category name (http://www.mydomain.com/product.html)

Is there a way to fix this?

I’m running the v1.1.8

 
Magento Community Magento Community
Magento Community
Magento Community
 
jimmypalmer
Sr. Member
 
Total Posts:  79
Joined:  2008-05-04
 

I also just noticed that sometimes the related products use the long-none-rewritten url:

http://localhost/catalog/product/view/id/3/s/product-name/category/3/

 
Magento Community Magento Community
Magento Community
Magento Community
 
jimmypalmer
Sr. Member
 
Total Posts:  79
Joined:  2008-05-04
 

Hmmmm....it seems to be getting worse...the items that are added to the cart all use the same long none rewritten url.

For some reason I thought this problem was resolved???

 
Magento Community Magento Community
Magento Community
Magento Community
 
impi_busy
Jr. Member
 
Avatar
Total Posts:  24
Joined:  2009-02-18
 

hey guys,

i have added my full category url for related products and upsell products.
this could probably work for stuff like the cart links too,

here is what i have done:

for related products:
edit app/design/frontend/default/YOURTHEME/template/catalog/product/list/related.phtml

I add the following code:

<div class="item">
                    <
div class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" /></div>
                    <
div class="product-details">
<!-- 
fix to bring in category URL -->
<?
                    $_categories 
$_item->getCategoryIds();
                       
$_category Mage::getModel('catalog/category')->load($_categories[1]);
                       
$url $this->getUrl($_category->getUrlPath($_item)).basename($_item->getProductUrl()); 
 
?>
<!-- Now bring in the new URL -->
<
h2 class="product-name"><a href="<?php echo $url ?><? /*echo $_item->getProductUrl()*/ ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></h2>

For upsell products I have done the same but here is my code anyways:

<?php for($_j=0;$_j<$this->getColumnCount();$_j++): ?>
            <?php 
if($_link=$this->getIterableItem()): ?>
            
<td>
                <
h2 class="product-name">
                
<!-- 
bring in our new category path -->
                    
<?
                    $_categories 
$_link->getCategoryIds();
                       
$_category Mage::getModel('catalog/category')->load($_categories[1]);
                       
$url $this->getUrl($_category->getUrlPath($_link)).basename($_link->getProductUrl()); 
                     
?>
                     
<!-- Call the new URL -->
                    <
a href="<? echo $url ?><?php /*echo $_link->getProductUrl()*/ ?>">
                        
<?php echo $this->htmlEscape($_link->getName()) ?>
                    
</a>
                </
h2>
<!-- and 
again here -->
                <
p><a href="<? echo $url ?><?php /*echo $_link->getProductUrl()*/ ?>"><img src="<?php echo $this->helper('catalog/image')->init($_link, 'small_image')->resize(125) ?>" width="125" height="125" alt="<?php echo $this->htmlEscape($_link->getName()) ?>" title="<?php echo $this->htmlEscape($_link->getName()) ?>" /></a></p>

This is works and require no core changes - it’s all done in theme so when you upgrade magento your changes will still be there.
You will also notice the [1] this was [0] in the original code but changing it to [1] brings in the complete category path.

I hope this will help some of you guys and girls

impi_busy

 
Magento Community Magento Community
Magento Community
Magento Community
 
Appmerce
Guru
 
Avatar
Total Posts:  376
Joined:  2008-05-13
Amsterdam, The Netherlands
 

My category URLs end with .html, the above solution gives me:

/category.html/product.html

Its not a really good solution, it doesnt get the real product url alias.

 
Magento Community Magento Community
Magento Community
Magento Community
 
liesegang
Jr. Member
 
Avatar
Total Posts:  23
Joined:  2008-08-19
 

use this to avoid .html after category url

$url substr($this->getUrl($_category->getUrlPath($_item)),0,-6).'/'.basename($_item->getProductUrl());
 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_lala
Member
 
Total Posts:  42
Joined:  2009-07-13
Germany
 

got it to work. had to adopt it a little, but now all my related / last viewed etc products have the correct url and breadcrumb/category. thanks!

Here is what worked for me so far:

Between <div class="product-details"> and the product name, I added the following lines to the related.phtml file (app/design/frontend/default/YOURTHEME/template/catalog/product/list/related.phtml) , oh and last line in the code is the new product name, so you can comment out the old one:

<!-- fix to bring in category URL -->
<?                
                    $_categories 
$_item->getCategoryIds();
            if( 
count($_categories) )
                {$_category 
Mage::getModel('catalog/category')->load($_categories[0]);
                       
$_categoryUrl $_category->getUrl();
                    
$url substr($_categoryUrl,0,-5).'/'.basename($_item->getUrlKey()).'.html';
                
}
                
else 
                
{                       $_category Mage::getModel('catalog/category')->load($_categories);
                       
$_categoryUrl $_category->getUrl();
                    
$url $this->getUrl($_category->getUrlPath($_item)).basename($_item->getUrlKey()).'.html';
                
} ?>
                
<a href="<?php echo $url ?><? /*echo $_item->getProductUrl()*/ ?>"<?php echo $this->htmlEscape($_item->getName()) ?></a>

<!-- 
end fix to bring in category URL -->

Do the same for app/design/frontend/default/YOURTHEME/template/reports/product_viewed.phtml. You can also do this to your search_list.phtml (or whatever new name you give your cloned products/view/list.phtml in catalogsearch.xml for the search list, i.e. change

<block type="catalog/product_list" name="search_result_list" template="catalog/product/list.phtml"></block>
list.phtml to search_list.phtml, otherwise your working product links might get the wrong category), and for the fix in the search_list.phtml replace the variable $_item in my code with $_product. afterwards users will be able go to an associated category of the product from the search page.
 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_lala
Member
 
Total Posts:  42
Joined:  2009-07-13
Germany
 
liesegang - 22 August 2009 12:32 AM

use this to avoid .html after category url

$url substr($this->getUrl($_category->getUrlPath($_item)),0,-6).'/'.basename($_item->getProductUrl());

if I switch on the block html output cache, the substr command is not really working anymore and the the url turns to http://www.pathtocategory.html?___/itemname.html

Now I get always redirected to the top category (pathtocategory.html) and not to the rest of the url adress. any helps with the php command?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brett
Sr. Member
 
Total Posts:  121
Joined:  2007-12-01
 

You only need to comment out one line of code to fix this entire issue on 1.4. Search my posts, I’m on a phone ATM. The solution I learned a couple daysxago, is clearly laid out in a [solved] thread. My issue was relating to the URL of products in parent categories when they exist in a child of that parent.  Hopefully it helps you guys too. The reason this information is so hard to come by is, that’s magentos profit model. People expect to make money answering questions like this. I guess I can’t blame them wink major props to the guy who helped me out with this issue. I’ll link the thread later..

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brett
Sr. Member
 
Total Posts:  121
Joined:  2007-12-01
 

http://www.magentocommerce.com/boards/viewthread/182328/

\"Brett\" -

Ok so it works. For version 1.4, this change is easy to make. Just go to \\app\\code\\core\\Mage\\Catalog\\Model\\Resource\\Eav\\Mysql4\\Product\\Collection.php

Look for line #906

->where(\'category_id=? OR category_id is NULL\'$this->_urlRewriteCategory)
comment that line with // and save the file

now your product urls should show up correctly even from the parent category.

HOWEVER,

I have no idea how this will work if the product belongs to more than one category, and that is probably the reason for this line of code. Does anyone else know how to?

----
Thanks so much furnitureforyoultd. I swear, every time i need to change something about magento, I spend about 3 days trying to find the one line of code i need to change… Once again, it was only one line of code and I didn\’t even need to change it.

Courtesy of furnitureforyoultd

 
Magento Community Magento Community
Magento Community
Magento Community
 
MDG-IT
Member
 
Avatar
Total Posts:  31
Joined:  2010-06-18
Melbourne, Australia
 

Brett - commenting out

->where('category_id=? OR category_id is NULL'$this->_urlRewriteCategory)
works for the catalogue but not for upsell items, at least for me.

With the category.html/product.html problem, I think the easiest solution is to just remove the category URL suffix in System - Configuration - Catalogy - SEO - unless someone thinks this would really have an impact on SEO?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nemosreef
Jr. Member
 
Total Posts:  13
Joined:  2010-10-04
 

This worked great but when i tried to implement into a bundled product, the links would either duplicate to match the number of products in the group...or would create a single link but it would be identical to the last simple product listed
this is the test site
http://nemosreef.us/products/aquariums-and-furniture/complete-aquariums/75-gallon-aquarium-setup.html

this is the code that makes it
<?php foreach ($_selections as $_item): ?>

<?
$_categories = $_item->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories);
$url = $this->getUrl($_category->getUrlPath($_item)).basename($_item->getProductUrl());
?>
<a href="<? echo $_item->getProductUrl() ?>"> <?php echo $this->htmlEscape($_item->getName()) ?></a>

<?php endforeach; ?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
haijerome
Member
 
Avatar
Total Posts:  44
Joined:  2010-07-19
 

Hi,

I have got the right URL for my Upsell products of both my stores (German) and (English) using the following.

Kindly Note this line

$category_id_upsell = count($_categories_upsell) - 1;

This line varies based on your Category and sub category order.

I hope this helps some one at some point of time. grin If so let me know

$storeId Mage::app()->getStore()->getId();
                  
$_categories_upsell $_link->getCategoryIds();
                  
$category_id_upsell count($_categories_upsell) - 1;
                  
$_category_upsell Mage::getModel('catalog/category')->setStoreId($storeId)->load($_categories_upsell[$category_id_upsell]);
                  
$get_url_Upsell $_category_upsell->setStoreId($storeId)->getUrl();
                  
$get_url_Upsell substr($get_url_Upsell,0,strpos($get_url_Upsell,".html"));
                  
$url $get_url_Upsell.'/'.basename($_link->getProductUrl());

Regards,
Jerome Dennis D
http://haijerome.wordpress.com

+919846219974

 
Magento Community Magento Community
Magento Community
Magento Community
 
haijerome
Member
 
Avatar
Total Posts:  44
Joined:  2010-07-19
 

Kindly Note :

The Above code works well for both simple and configurable products. I haven’t tried with bundled products.

Regards,
Jerome Dennis D
http://haijerome.wordpress.com

+919846219974

 
Magento Community Magento Community
Magento Community
Magento Community
 
haijerome
Member
 
Avatar
Total Posts:  44
Joined:  2010-07-19
 

This is the latest solution that i’ve found for the right product URL for the upselling products but it displays the category URL too.

I hope it helps some one some time

<?php    
$model_upsell 
Mage::getModel('catalog/product'); //getting product model
$_product_upsell $model_upsell->load($_link->getId()); //getting product object for particular product id
 
$url =  $_product_upsell->getProductUrl();  ?>

Regards,
Jerome Dennis D
http://haijerome.wordpress.com
+919846219974

 
Magento Community Magento Community
Magento Community
Magento Community
 
cosmin_purcarea
Jr. Member
 
Total Posts:  16
Joined:  2010-09-28
 

Hi, does anyone know, how I can filter Upsell Collection by category.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2