Jump to content

Redirect to first subcategory if no products on that category.


Recommended Posts

Hello everybody.

So I'm trying to change the category module.

What I need is:

 

- If i Select a category, and that category has no products on it, instead of listing its sub categories, it should open the first subcategory and show its products.

 

Can somebody please give me an hint?

 

Thank you very much.

 

Miguel.

 

 

EDIT: Solved it. Thanks.

Edited by lordcracker (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

Hi - could you share your solution

 

I have the following situation:

 

Cat--1

|

|-------------------Cat--1.1

|----------------------------Cat--1.1.1

|-------------------------------------Cat--1.1.1.1

|----------------------------------------------Product 001

|----------------------------------------------Product 002

|----------------------------------------------Product 003

|----------------------------------------------Product 004

|--------------------------------------Cat--1.1.1.2

 

|----------------------------------------------NO PRODUCTS

 

|--------------------------------------Cat--1.1.1.3

 

|----------------------------------------------Product 008

|----------------------------------------------Product 009

|----------------------------------------------Product 010

 

|----------------------------Cat--1.1.2

|---------------------------------------Cat--1.1.2.1

 

|----------------------------------------------Product 011

|----------------------------------------------Product 012

|----------------------------------------------Product 013

 

|-------------------------------------Cat--1.1.2.2

|----------------------------------------------NO PRODUCTS

|-------------------------------------Cat--1.1.2.3

 

|----------------------------------------------Product 015

|----------------------------------------------Product 016

|----------------------------------------------Product 018

 

|----------------------------Cat--1.1.3

|-------------------Cat--1.2

|----------------------------Cat--1.2.1

|----------------------------Cat--1.2.2

|-------------------Cat--1.3

|----------------------------Cat--1.3.1

|----------------------------Cat--1.3.2

|-------------------Cat--1.4

|----------------------------Cat--1.4.1

|

Cat--2

 

|

|-------------------Cat--2.1

|----------------------------Cat--2.1.1

|-------------------Cat--2.2

|----------------------------Cat--2.2.1

|----------------------------Cat--2.2.2

|-------------------Cat--2.3

|-------------------Cat--2.4

|

 

 

 

 

Now I want a user to select in drop-down:

 

[Cat--1] [Cat--1.1.1] [Cat--1.1.2.3]

 

 

in the 2nd and 3d dropdown I do not want to show options for the categories that do not have any products.

 

 

Question - could your solution work or should I write my own MySQL queries

 

thanks

Edited by jacograaff (see edit history)
Link to comment
Share on other sites

I don't know if this can help you, but his is the code i added to controllers/front/CategoryController.php

On function initContent();

 

Between

 

if ($this->category->id != 1)
$this->assignProductList();

 

AND

 

$this->context->smarty->assign(array(

 

 

//MY EDIT! HAS PRODUCTS??
if(isset($this->cat_products) && $this->cat_products) {
//IT HAS PRODUCT! DO NOTHING
} else {
//HAS NO PRODUCTS, WHAT ABOUT SUBCATEGORIES??
 //CHECK DATABASE FOR SUBCATEGORIES
 $query = "SELECT * FROM `"._DB_PREFIX_."category` WHERE id_parent=".$this->category->id;
 $result = Db::getInstance()->ExecuteS($query);
 $nr_de_subcategorias = count($result);
 if($nr_de_subcategorias > 0) {
	//IT HAS SUBCATEGORIES! REDIRECT;
	$id_para_redirect = $this->context->smarty->tpl_vars['categories']->value[0]['id_category'];
	$newUrl = 'index.php?id_category='.$id_para_redirect.'&controller=category';
	//echo $newUrl;
	tools::redirect($newUrl);
} else {
	//DO NOTHING
}
}//DONE

Edited by lordcracker (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...