Forums / Templates, Stylesheets, Page Layout / Displaying products in sub categories

Displaying products in sub categories

Views: 1,165

Results 1 to 4 of 4
10 Nov 2011, 3:59 PM
#1
strelitzia avatar

strelitzia

Totally Zenned

Join Date:
Nov 2005
Location:
France
Posts:
588
Plugin Contributions:
1

Displaying products in sub categories

I'm struggling to find a solution to my problem, and i'm hoping someone can come up with a clever solution.

The scenario:
Each category has sub categories. Each sub category has further sub categories (sub sub categories).

Click on the category, i want all sub categories to show..... Zen cart does this as standard.
Click a sub category, i want it to display the products in each of the sub sub categories. As standard, Zen cart will just show yet another list of sub categories.

I'm not sure of the best way around this.

Zen Cart developer since 2009 — Zen Cart Solutions
Ecommerce development, integrations & automation — Junction Solutions

11 Nov 2011, 11:32 AM
#2
strelitzia avatar

strelitzia

Totally Zenned

Join Date:
Nov 2005
Location:
France
Posts:
588
Plugin Contributions:
1

Re: Displaying products in sub categories

Ok, so i decided to put in a flag that is set once the system has displayed the first level of sub categories, so that any subsequent request would follow a different SQL query.
Problem is that although the flag gets set to 1 as soon as displays a sub category, when the sub sub categories are displayed, the flag has been reset and it doesn't know it was already 1.

This is the code i have in includes/modules/pages/index/header_php.php

<?php /** * index header_php.php * * @package page * @copyright Copyright 2003-2006 Zen Cart Development Team * @copyright Portions Copyright 2003 osCommerce * @license <http://www.zen-cart.com/license/2_0.txt> GNU Public License V2.0 * @version $Id: header_php.php 4371 2006-09-03 19:36:11Z ajeh $ */ // This should be first line of the script: $zco_notifier->notify('NOTIFY_HEADER_START_INDEX'); echo ' catflag when index_header_php loads is '. $catflag;//check catflag status when page loads ***REMOVE THIS LINE LATER*** // the following cPath references come from application_top/initSystem $category_depth = 'top'; if (isset($cPath) && zen_not_null($cPath)) { $categories_products_query = "SELECT count(*) AS total FROM " . TABLE_PRODUCTS_TO_CATEGORIES . " WHERE categories_id = :categoriesID"; $categories_products_query = $db->bindVars($categories_products_query, ':categoriesID', $current_category_id, 'integer'); $categories_products = $db->Execute($categories_products_query); if ($categories_products->fields['total'] > 0) { $category_depth = 'products'; // display products } else { $category_parent_query = "SELECT count(*) AS total FROM " . TABLE_CATEGORIES . " WHERE parent_id = :categoriesID"; $category_parent_query = $db->bindVars($category_parent_query, ':categoriesID', $current_category_id, 'integer'); $category_parent = $db->Execute($category_parent_query); if (($category_parent->fields['total'] > 0) && ($catflag == 1)) { echo 'this is now nestedproducts level';//check the new level is being set when displaying sub sub categories ***REMOVE THIS LINE LATER*** $category_depth = 'nestedproducts'; // create new nestedproducts query in main_template_vars.php to show all products if this statement is true }elseif ($category_parent->fields['total'] > 0) { echo ' catflag before nested is '. $catflag; //check catflag status before category depth is set to nested ***REMOVE THIS LINE LATER*** $category_depth = 'nested'; $catflag=1; echo ' catflag after nested is '. $catflag;// navigate through the categories } else { echo 'catflag is'. $catflag;//check catflag status after category depth is set to nested ***REMOVE THIS LINE LATER*** $category_depth = 'products'; // category has no products, but display the 'no products' message } } } // include template specific file name defines $define_page = zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . '/html_includes/', FILENAME_DEFINE_MAIN_PAGE, 'false'); require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php')); // set the product filters according to selected product type $typefilter = 'default'; if (isset($_GET['typefilter'])) $typefilter = $_GET['typefilter']; require(DIR_WS_INCLUDES . zen_get_index_filters_directory($typefilter . '_filter.php')); // query the database based on the selected filters $listing = $db->Execute($listing_sql); // if only one product in this category, go directly to the product page, instead of displaying a link to just one item: // if filter_id exists the 1 product redirect is ignored if (SKIP_SINGLE_PRODUCT_CATEGORIES=='True' and (!isset($_GET['filter_id']) and !isset($_GET['alpha_filter']))) { if ($listing->RecordCount() == 1) { zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id'])); } } $catflag = $_POST["catflag"]; // This should be last line of the script: $zco_notifier->notify('NOTIFY_HEADER_END_INDEX'); ?>

How can i get it to remember the $catflag.

I assume it has to be set as a session id somewhere?

Zen Cart developer since 2009 — Zen Cart Solutions
Ecommerce development, integrations & automation — Junction Solutions

11 Nov 2011, 12:32 PM
#3
strelitzia avatar

strelitzia

Totally Zenned

Join Date:
Nov 2005
Location:
France
Posts:
588
Plugin Contributions:
1

Re: Displaying products in sub categories

Got the switch working...of sorts.

Put this into init_sessions.php

//set $_SESSION['catflag'] if not previously set
if (!isset($_SESSION['catflag'])) {
$_SESSION['catflag'] = 0;
}
//end

Used this for header_php.php

<?php /** * index header_php.php * * @package page * @copyright Copyright 2003-2006 Zen Cart Development Team * @copyright Portions Copyright 2003 osCommerce * @license <http://www.zen-cart.com/license/2_0.txt> GNU Public License V2.0 * @version $Id: header_php.php 4371 2006-09-03 19:36:11Z ajeh $ */ // This should be first line of the script: $zco_notifier->notify('NOTIFY_HEADER_START_INDEX'); $catflag = $_SESSION[catflag]; echo ' catflag when index_header_php loads is '. $catflag;//check catflag status when page loads ***REMOVE THIS LINE LATER*** // the following cPath references come from application_top/initSystem $category_depth = 'top'; if (isset($cPath) && zen_not_null($cPath)) { $categories_products_query = "SELECT count(*) AS total FROM " . TABLE_PRODUCTS_TO_CATEGORIES . " WHERE categories_id = :categoriesID"; $categories_products_query = $db->bindVars($categories_products_query, ':categoriesID', $current_category_id, 'integer'); $categories_products = $db->Execute($categories_products_query); if ($categories_products->fields['total'] > 0) { $category_depth = 'products';$catflag=0; } else { $category_parent_query = "SELECT count(*) AS total FROM " . TABLE_CATEGORIES . " WHERE parent_id = :categoriesID"; $category_parent_query = $db->bindVars($category_parent_query, ':categoriesID', $current_category_id, 'integer'); $category_parent = $db->Execute($category_parent_query); if (($category_parent->fields['total'] > 0) && ($catflag == 1)) { echo 'this is now nestedproducts level';//check the new level is being set when displaying sub sub categories ***REMOVE THIS LINE LATER*** $catflag=0; $category_depth = 'nestedproducts'; // create new nestedproducts query in main_template_vars.php to show all products if this statement is true }elseif ($category_parent->fields['total'] > 0) { echo ' catflag before nested is '. $catflag; //check catflag status before category depth is set to nested ***REMOVE THIS LINE LATER*** $category_depth = 'nested'; $catflag=1; echo ' catflag after nested is '. $catflag;// navigate through the categories } else { echo 'catflag is'. $catflag;//check catflag status after category depth is set to nested ***REMOVE THIS LINE LATER*** $category_depth = 'products'; // category has no products, but display the 'no products' message } } } // include template specific file name defines $define_page = zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . '/html_includes/', FILENAME_DEFINE_MAIN_PAGE, 'false'); require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php')); // set the product filters according to selected product type $typefilter = 'default'; if (isset($_GET['typefilter'])) $typefilter = $_GET['typefilter']; require(DIR_WS_INCLUDES . zen_get_index_filters_directory($typefilter . '_filter.php')); // query the database based on the selected filters $listing = $db->Execute($listing_sql); // if only one product in this category, go directly to the product page, instead of displaying a link to just one item: // if filter_id exists the 1 product redirect is ignored if (SKIP_SINGLE_PRODUCT_CATEGORIES=='True' and (!isset($_GET['filter_id']) and !isset($_GET['alpha_filter']))) { if ($listing->RecordCount() == 1) { zen_redirect(zen_href_link(zen_get_info_page($listing->fields['products_id']), ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing->fields['products_id'])); } } $_SESSION[catflag]=$catflag; echo 'this is now the end of header_php and catflag is ' . $catflag;//check the catflag level when header_php script ends ***REMOVE THIS LINE LATER*** // This should be last line of the script: $zco_notifier->notify('NOTIFY_HEADER_END_INDEX'); ?>

the flag is set when it should be and will enable a new script to be run for displaying sub sub category images....

except one issue:

If you navigate back to the home page, or directly to another category then catflag is still set at 1.

Any way of resetting the flag when index or a new category is loaded?

When i get this working i'll post it for others as it should prove useful.

Thanks for any help.

Zen Cart developer since 2009 — Zen Cart Solutions
Ecommerce development, integrations & automation — Junction Solutions

11 Nov 2011, 7:05 PM
#4
strelitzia avatar

strelitzia

Totally Zenned

Join Date:
Nov 2005
Location:
France
Posts:
588
Plugin Contributions:
1

Re: Displaying products in sub categories

41 views and not one response?

Maybe this is something more difficult to achieve than i thought.

I'll keep working on it and post any updates i have in case it helps someone else out.

Zen Cart developer since 2009 — Zen Cart Solutions
Ecommerce development, integrations & automation — Junction Solutions