Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Redirect To Product Page If There Is Only One Product In Category Rate Topic   - - - - -

 
  • raff
  • Member
  • Members
  • Join Date: 23-Feb 12
  • 21 posts

Posted 09 April 2014 - 12:10 PM #1

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

 
  • Hungryweb
  • Senior Member
  • Authorized Reseller
  • Join Date: 10-Feb 12
  • 1244 posts

Posted 09 April 2014 - 12:32 PM #2

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
part of hungryweb.net

 
  • raff
  • Member
  • Members
  • Join Date: 23-Feb 12
  • 21 posts

Posted 09 April 2014 - 01:04 PM #3

Valentin, you a STAR!
Works just as required. Brilliant.
Thank you.

kind regards
raff

 
  • Hungryweb
  • Senior Member
  • Authorized Reseller
  • Join Date: 10-Feb 12
  • 1244 posts

Posted 09 April 2014 - 01:07 PM #4

Thank you and You are welcome

---
Valentin
part of hungryweb.net

 
  • dvsgr
  • Senior Member
  • Authorized Reseller
  • Join Date: 14-Apr 09
  • 1446 posts

Posted 29 August 2016 - 08:02 AM #5

HI Guys

 

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

<?php

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


Fotis Kourmadas
Cs-Cart Development - Cs-Cart Authorized Reseller 

 

Posted 28 June 2017 - 06:54 AM #6

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

Cheers,

SWiM.



 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 19056 posts

Posted 02 March 2018 - 12:55 PM #7

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


GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    CS-Cart RU                         24500 руб.
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      CS-Cart RU + UniTheme    36000 руб.


 
  • Erol
  • Member
  • Members
  • Join Date: 26-Feb 17
  • 41 posts

Posted 18 February 2019 - 06:37 PM #8

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



 
  • Hungryweb
  • Senior Member
  • Authorized Reseller
  • Join Date: 10-Feb 12
  • 1244 posts

Posted 19 February 2019 - 05:25 AM #9

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

<?php
/*
 * © 2019 Hungryweb
 * 
 * @website: www.hungryweb.net
 * @support: support@hungryweb.net
 *  
 */

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...

Edited by Hungryweb, 21 February 2019 - 05:44 AM.


 
  • Erol
  • Member
  • Members
  • Join Date: 26-Feb 17
  • 41 posts

Posted 20 February 2019 - 09:37 PM #10

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



 
  • Hungryweb
  • Senior Member
  • Authorized Reseller
  • Join Date: 10-Feb 12
  • 1244 posts

Posted 21 February 2019 - 05:46 AM #11

@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 ;)



 
  • Erol
  • Member
  • Members
  • Join Date: 26-Feb 17
  • 41 posts

Posted 21 February 2019 - 09:16 AM #12

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