Redirect To Product Page If There Is Only One Product In Category

As in title, does anyone know how to set automatic redirection to a product if there is only one product in given category?

Say customer clicks on the category link from main drop-down menu, but if there is only one product in that category, he/she is taken directly to product page.



TIA

raff



PS. Using version 4.1.3

Hi @raff,

For this to work you need to have My changes add-on enabled



#add into file app/addons/my_changes/controllers/frontend/categories.post.php (if not exist, please create, folder structure and php file )


```php

/***************************************************************************
* *
* (c) 2014 HungryWeb.net | Support at yum@hungryweb.net *
* *
***************************************************************************/
use Tygh\Registry;
if (!defined('BOOTSTRAP')) { die('Access denied'); }
if($mode == 'view'){
if (!defined('AJAX_REQUEST')) {
//get page data
$products = Registry::get('view')->getTemplateVars('products');
if(empty($products[1]) && !empty($products[0])){
return array(CONTROLLER_STATUS_REDIRECT, "products.view?product_id=".$products[0]['product_id']);
}
}

}
```

I hope that helps,

---
Valentin
[color=#808080][size=2]part of hungryweb.net[/size][/color]

Valentin, you a STAR!

Works just as required. Brilliant.

Thank you.



kind regards

raff

Thank you and You are welcome





Valentin

[color=#808080][size=2]part of hungryweb.net[/size][/color]

HI Guys

an updated version I needed for 43x (in 438 tested)

use Tygh\Registry;

if (!defined(‘BOOTSTRAP’)) { die(‘Access denied’); }

if($mode == ‘view’){
if (!defined(‘AJAX_REQUEST’)) {
$products = Registry::get(‘view’)->getTemplateVars(‘products’);
if(count($products) == 1) {
$product = array_shift($products);
return array(CONTROLLER_STATUS_REDIRECT, “products.view?product_id=”.$product[‘product_id’]);
}
}
}​

Fotis

Thanks very much for posting this DVSGR! Helped us out a great deal.

Cheers,

SWiM.

not sure it still works, but thanks both of you guys! you guys are amazing, I will need to check this later, thanks!

Our free Search Improvements module has the single product redirect option

Hi Guys,

sorry to inform you that NONE of above solutions works.

1. Solution 1 (HungryWeb and Fotis) gives error SERVICE UNAVAILABLE

2. Solution 2 (eComLabs addon) is NOT solution, bec it only works if you search for it.

If you have it as a link in your menu, it will open Categorie instead of opening Product, even if you have only 1 product in that categorie.

Does anyone know working solution to open single product when customer click on category with single product?

Thanks

Erol

My changes add-on should be enabled for this to work

#file

app/addons/my_changes/controllers/frontend/categories.post.php

* if don not exist, please create, folder structure and php file


#content

if ( !defined(‘BOOTSTRAP’) ) { die(‘Access denied’); }

use Tygh\Registry;

if(!defined(‘AJAX_REQUEST’) && $mode == ‘view’ && !empty($_REQUEST[‘category_id’])){
$products = Tygh::$app[‘view’]->getTemplateVars(‘products’);
if(!empty($products) && count($products)==1){
$products = reset($products);
return array(CONTROLLER_STATUS_REDIRECT, “products.view?product_id=”.$products[‘product_id’]);
}
}

* Tested on 4.9.2

also you can choose other redirect header

// Controller return statuses
define('CONTROLLER_STATUS_REDIRECT', 302);
define('CONTROLLER_STATUS_OK', 200);
define('CONTROLLER_STATUS_NO_PAGE', 404);
define('CONTROLLER_STATUS_DENIED', 403);
define('CONTROLLER_STATUS_DEMO', 401);

* to change the redirect header just change constant

change:

return array(CONTROLLER_STATUS_REDIRECT, "products.view...

to:

return array(CONTROLLER_STATUS_OK, "products.view...

Dear Hungryweb,

thanks for help..

I would like to use option 2 REDIRECT "also you can choose other redirect header"

Still, I am not sure where to place those Controller statuses and how to implement that file in cs-cart..

What should I do with it?

Also, will that redirect be funkcional even in future updates of cs-cart.

Thanks

Erol

@Erol I edit the post and now you have all the details needed to make it work

Also, will that redirect be funkcional even in future updates of cs-cart.


I am not able to know what can come with 5.x version but should work just fine with future CS-Cart upgrades ;)

Hi,

Thanks for update.

Still do not get it..

Where to change

* to change the redirect header just change constant ?

Where is that file on my server or on my cs-cart?

How to find file to change?

Thanks

Erol