Category w/ 1 Product Redirect ?

I have a couple subcategories that only have 1 product attributed to them.



For example “Food & Wine” have a subcategory called “Wine Club”. In “Wine Club” there is only 1 product.



How can I configure my store so that when someone clicks on the subcategory “Wine Club” it takes them directly to that 1product’s detail page (rather than a product grid page)?



Any suggestions?



The subcategories must stay for navigation purposes, so simply removing them will not work.

Sure, if the count of $products in your category page is 1 then issue the redirect. Probably best to catch this in a addons/my_changes/controllers/categories.post.tpl file and look at the count of products in that category. If one, redirect to a product page.

[quote name=‘tbirnseth’]Sure, if the count of $products in your category page is 1 then issue the redirect. Probably best to catch this in a addons/my_changes/controllers/categories.post.tpl file and look at the count of products in that category. If one, redirect to a product page.[/QUOTE]



Thank you for your response. Would you be able to supply me with proper code for doing this? I am not very fluent in changing back-end code.

[quote name=‘Chef_Joe’]Thank you for your response. Would you be able to supply me with proper code for doing this? I am not very fluent in changing back-end code.[/quote]



maybe tbirnseth can get this to work for you:

```php

{if $category_data.product_count == 1}


{else}

{/if}

```

Actually this is a much better method:



OPEN /controllers/customer/categories.php



At the very bottom AFTER the last } PASTE:



if (empty($categories) && count($products) == 1) {
fn_redirect("$index_script?dispatch=products.view&product_id=".$products[0]['product_id']);
}
SAVE.

It would be better done as a addons/my_changes/controllers/customers/categories.post.php



Why change a core file when you don’t need to?

That works great Sno, thank you very much for posting that.



Brandon

[quote name=‘tbirnseth’]It would be better done as a addons/my_changes/controllers/customers/categories.post.php



Why change a core file when you don’t need to?[/quote]



It’s hard to change old timers :slight_smile:



Actually I did do exactly as you posted and I couldn’t get it to work, I’m not sure why but I tested on several installs and it just wouldn’t work. The system was reading the file addons/my_changes/controllers/customers/categories.post.php but for some reason it wasn’t able to get the product count for it to work.

  • Sno

It’s being run inside fn_run_controllers() function, hence if you want to look at the $products or $categoreis variables, you have to read them from the template engine like


$products = $view->get_var('products');




This will get you the variables that the main controller has already set for the page. Of course you could always read the DB again and see if the category_id has any sub_cats or whether there are any products associated with it. But that work was already done and assigned to template variables. The get_var() is all memory to memory operations so performance is quite good.

[quote name=‘snorocket’]Actually this is a much better method:



OPEN /controllers/customer/categories.php



At the very bottom AFTER the last } PASTE:



if (empty($categories) && count($products) == 1) {
fn_redirect("$index_script?dispatch=products.view&product_id=".$products[0]['product_id']);
}
SAVE.[/QUOTE]



Thanks sno. I did exactly as you said and everything works great! Nothing looks worse than going to a subcategory page with only 1 product on it.



The only issue I see now is that this fix still leaves a double entry in the breadcrumbs (2 breadcrumb links that link to the same page). This is a very minor point, but something hopefully someone can create a fix for. Thanks again!

[quote name=‘Chef_Joe’]

The only issue I see now is that this fix still leaves a double entry in the breadcrumbs (2 breadcrumb links that link to the same page). This is a very minor point, but something hopefully someone can create a fix for. Thanks again![/quote]

I can’t reproduce this error, can you post a screenshot?



Thanks - Sno

Sure, no problem.





Arrow 1 in the breadcrumbs is the subcategory that we applied the redirect to.



Arrow 2 in the breadcrumbs is the actual name of the product.



Since we applied the redirect to the subcategory, both arrows 1 & 2 are now the same link. No matter which one you click on it will bring you to the product: Sirloin of the Month Club.



Here is link to actual page:



Like I said before, this is a very minor issue. But all fixes help :slight_smile:

@ snorocket

i used your code and it works just fine.

i noticed only one problem. if you limit the page view for example to 6 products on the catagory page, and the category has 7 products, than the customer can not go to the second page, wher only one product is left.



he can click the second page but nothing will happen.



If there is one product left on the last page i have always this problem.



so the question is, is it only one problem i have, or has nobody so far noticed ?



i have no clue how to fix it in my case and need help.



Thanks

Did anybody ever get this working properly?