Log in

View Full Version : Redirect to Product Page when only One product in category


Pace
2016-02-14, 07:40 PM
For simple sites that don't use sub-categories, etc, this may come in handy! Further, this was my first success at defining a Smarty var in the actual .TPL template file, then referencing back in php. I'm sure there is an easier way to do this, but this was my first successful attempt.

A request was made to have all of our category links on the left link directly to the individual product page when there is only one product in the category.

Adding the following code to the top of skin1/customer/main/subcategories.tpl does the trick!

Thanks to darrenkierman for pointing out that adding the following code to the top of skin1/customer/main/search_result.tpl will also take you straight to the product page if your search returns only 1 result!

{if $products|@count eq 1}
{assign var="prodURL" value="product.php?productid=`$products.0.productid`&cat=$cat&page=1"}
{php}
header( "Location: " . $GLOBALS[smarty]->_tpl_vars[prodURL] );
{/php}
{/if}

Raelyn
2016-02-14, 07:41 PM
One small bug .... lets say you have one product and 5 subcategories, it will not show the subcategories because it automatically jumps to the one product. To fix it :

{if $products|@count eq 1}
{if $subcategories}{else}
{assign var="prodURL" value="product.php?productid=`$products.0.productid`&cat=$cat&page=1"}
{php}
header( "Location: " . $GLOBALS[smarty]->_tpl_vars[prodURL] );
{/php}
{/if}
{/if}

Taberville
2016-02-14, 07:44 PM
Just a note, if your using CDSEO then:
Replace:

{assign var="prodURL" value="product.php?productid=`$products.0.productid`&cat=$cat&page=1"}


with:

{assign var="prodURL" value="`$http_location`/`$cdseo_prod`/`$products.0.cdseo_url`"}