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 3
redirect “child” (simple) products to “parent” (configurable) products
 
afmaury
Sr. Member
 
Total Posts:  100
Joined:  2008-01-05
 

I am trying to find a way to redirect the url for a simple product to go to the corresponding configurable product. For example if I have a link to a small blue t-shirt, it would go to the main t-shirt page where the attributes are available to select.

Right now, all simple products that are part of configurable products are set to visibility = nowhere so the url just produces a 404 error. Instead, I’d like the url to redirect to the configurable product.

I need this because we publish feeds for resellers that have all items listed (not just products with a visibility of search and/or catalog). The resellers need to see if a particular item is in stock via the feed.

Does anyone have any ideas on how to do this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jakub@WebCatch
Sr. Member
 
Avatar
Total Posts:  215
Joined:  2009-11-06
Nottingham, UK
 

Enable the simple product pages and put some additional code in your product page (template/catalog/product/view.phtml). In this code you will need to check if the displayed product is a simple product and if so, redirect to the corresponding configurable product’s page.

The code should look something like this:

//get parent (configurable) ids
$parentId $_product->loadParentProductIds()->getData('parent_product_ids');
if(isset(
parentId[0])){//if this product has a parent
  
$parent Mage::getModel('catalog/product')->load($parentId[0]);
  echo 
"<script>window. location='".$parent->getProductUrl()."';</script>"
}

Unfortunately you have to do a javascript redirect, because the php output is already started when view.phtml is interpreted, so changing the header from this file is not possible.

Note: I had to put the space between window. and location, because it was censored by the forum.

 
Magento Community Magento Community
Magento Community
Magento Community
 
afmaury
Sr. Member
 
Total Posts:  100
Joined:  2008-01-05
 

Thanks for your help with this. I haven’t had a chance to try it out yet, but it looks good. When you said to enable the simple products, did you mean to make their visibility search?

If that is what you meant, there is a workaround for that as well in another thread. That allows you to keep the product visibility set to nowhere, but still have the url work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jakub@WebCatch
Sr. Member
 
Avatar
Total Posts:  215
Joined:  2009-11-06
Nottingham, UK
 

Yeah, by enable I meant that their urls need to be accessible. They don’t have to, and really shouldn’t be visible anywhere on your site. Thanks for this very useful link! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
afmaury
Sr. Member
 
Total Posts:  100
Joined:  2008-01-05
 

No problem. Hopefully this will help others out as well.

 
Magento Community Magento Community
Magento Community
Magento Community
 
afmaury
Sr. Member
 
Total Posts:  100
Joined:  2008-01-05
 

Ok, just had a chance to get this working. In case anyone else wants to do this, here is what I did:

1) Make all simple “child” products visible by changing the code referenced in this thread:
http://www.magentocommerce.com/boards/viewthread/43561/

2) Open the file at app/design/frontend/default/default/template/catalog/product/view.phtml

3) Find the following code:

$_helper $this->helper('catalog/output');
    
$_product $this->getProduct();

4) Insert this code after it:

//redirect to parent product if a child product
    $parentId $_product->loadParentProductIds()->getData('parent_product_ids');
    if(isset(
$parentId[0]))
    
{
          $parent 
Mage::getModel('catalog/product')->load($parentId[0]);
          echo 
"<script>window. location='".$parent->getProductUrl()."';</script>"
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Jakub@WebCatch
Sr. Member
 
Avatar
Total Posts:  215
Joined:  2009-11-06
Nottingham, UK
 

Found an even better way of dealing with that - instead of doing a javascript redirect, you can modify the product controller and do the “redirect” there. The advantage is that it will be much faster (in the previous approach you basically need to load two different pages) and you won’t need to do any tricks to enable simple product pages…

The file you need to modify is /app/code/core/Mage/Catalog/controllers/ProductController and the method name is _initProduct().

Find the code which retrieves the product to be displayed:

$product Mage::getModel('catalog/product')
            ->
setStoreId(Mage::app()->getStore()->getId())
            ->
load($productId);

And add this code after it:

if($product->type_id=="simple"){
        $parentId 
$product->loadParentProductIds()->getData('parent_product_ids');
        if(isset(
$parentId[0]))
        
{
            $product 
Mage::getModel('catalog/product')->load($parentId[0]);
        
}
    }

It is almost identical to the code we used before on the product page. The difference is that you do not need to do any sort of redirect, you just change the $product variable from the simple product to a corresponding configurable product.

That’s it, simple and elegant.

 
Magento Community Magento Community
Magento Community
Magento Community
 
gurubob
Jr. Member
 
Total Posts:  7
Joined:  2009-01-27
 
Jakub@WebCatch - 19 March 2010 07:13 AM


It is almost identical to the code we used before on the product page. The difference is that you do not need to do any sort of redirect, you just change the $product variable from the simple product to a corresponding configurable product.

That’s it, simple and elegant.

I don’t know what effect this will have on SEO - the URL to the old product will still be live and valid whereas the redirect method has the advantage that you can control whether clients/robots etc will be permanently or temporarily redirected to the new URL.

Good spotting though, I’ll be using your method myself.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Matt Dean
Guru
 
Avatar
Total Posts:  549
Joined:  2008-05-27
 

Nice work chaps, I’ll no doubt need to use that at some point. It’s always nice when you find a pre-done solution to things like this smile

Only little thing I’d point out (which I bet you know but others might not) is that it’s possible for a product to have more than 1 parent. Quite how you would handle that any better than you currently do I’m not sure. (presumably the order that the parentIds come back in is pretty consistent, so you always redirect to the same parent?)

I suppose you could possibly do it with a custom attribute which points at the ‘preferred’ parent product or something, but it’s probably not worth the hassle.

Matt

 
Magento Community Magento Community
Magento Community
Magento Community
 
eCom
Member
 
Total Posts:  53
Joined:  2008-04-20
 

Guys, can you help me to do the following?

The objective is to show all colors of a product on the search and catalog. So I’ve created a configurable product with visibility set to search only while all the child products’ visibility was set to visible for catalog and search.

How can the simple products to link to the parent products with the \"selected attribute\” already in place?
ie.  user see\’s in the category, the list of all t-shirts showing all 4 colors of the configurable product>, and when clicking on one of them, he will reach the configured product (as done in the code in previous post), while the color is already selected <via drop box, and thus also the image, (I used this article to do it>

Thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
 
jpk.in
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2009-08-12
Cochin, India
 

thanks for the info
resolved issue regarding configurable products in bestsellers…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Fortimir
Member
 
Total Posts:  35
Joined:  2009-05-09
 

Is there a way to redirect only on configured items but NOT on grouped items?  We have ran into a situation where we need to access the individual item’s info from the grouped page, but don’t want that to be possible on the items we have set up as configurable. Is this possible?

 
Magento Community Magento Community
Magento Community
Magento Community
 
kolomayah
Jr. Member
 
Total Posts:  4
Joined:  2011-07-04
 

thanks foor share

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tacgear
Member
 
Total Posts:  52
Joined:  2009-06-14
 
afmaury - 02 February 2010 05:45 PM

Ok, just had a chance to get this working. In case anyone else wants to do this, here is what I did:

1) Make all simple “child” products visible by changing the code referenced in this thread:
http://www.magentocommerce.com/boards/viewthread/43561/

2) Open the file at app/design/frontend/default/default/template/catalog/product/view.phtml

3) Find the following code:

$_helper $this->helper('catalog/output');
    
$_product $this->getProduct();

4) Insert this code after it:

//redirect to parent product if a child product
    $parentId $_product->loadParentProductIds()->getData('parent_product_ids');
    if(isset(
$parentId[0]))
    
{
          $parent 
Mage::getModel('catalog/product')->load($parentId[0]);
          echo 
"<script>window. location='".$parent->getProductUrl()."';</script>"
    
}

Inserting this code does nothing in 1.5.1, all I really want is something that reads “View this item with all of it’s options.” Not even a redirect but I can’t get this to echo anything. /sadface

 
Magento Community Magento Community
Magento Community
Magento Community
 
leesoes
Jr. Member
 
Avatar
Total Posts:  3
Joined:  2011-07-09
 

I think this is the weak point of magento

 
Magento Community Magento Community
Magento Community
Magento Community
 
leesoes
Jr. Member
 
Avatar
Total Posts:  3
Joined:  2011-07-09
 

I also face the same problem, have you got the answer?

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