X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Product is in these categories (https://forum.x-cart.com/showthread.php?t=21983)

jamesc 05-22-2006 09:50 AM

Product is in these categories
 
This was just a quick hack to get a result I needed for a product display page. It's messy and I'm sure someone will tidy it up and make it nice, but someone may be trying to do the same thing.

I have a selection of DVDs organised by studio using the manufacturer feature of X-Cart 4.0.15. Then my categories include things such as "comedy", "action", "thriller", etc. On the product display page I want a clickable graphic representation of the categories each DVD is in (comedy thriller, for example). So I put this quick botch in to product.php:

Code:

$cat_result = db_query(" SELECT xcart_products_categories.categoryid,xcart_categories.category FROM xcart_products_categories, xcart_categories WHERE xcart_products_categories.productid = $productid AND xcart_categories.categoryid = xcart_products_categories.categoryid");

while ($category = db_fetch_row($cat_result)) {
        if ($category) { $product_categories[] = $category; }
}

$smarty->assign("product_categories",$product_categories);


In product.tpl I can now loop through $product_categories and use {$product_categories[loop].0} for each category id and {$product_categories[loop].1} for each category name. Or, in the case I'll be using it, .0 to provide an icon and href and .1 for the alt text.

Hope that helps someone. Sorry it's so messy; I should probably use the correct variables for table/column names but .... YMMV.

:)

James.

balinor 05-22-2006 10:00 AM

Moving to Custom Mods, thanks for contributing!

dgreen 05-29-2006 07:48 AM

sample code, please
 
Could you post exactly how to use this? I think this is what I have been trying to do but I couldn't figure out how to implement it.
Thanks

jamesc 05-29-2006 11:29 AM

Re: sample code, please
 
Quote:

Originally Posted by dgreen
Could you post exactly how to use this? I think this is what I have been trying to do but I couldn't figure out how to implement it.


This is a very quick and dirty solution which doesn't use the "xcart" way of creating database queries by using variable names, but it works. I inserted the following code at line 143 of "product.php":

Code:

$cat_result = db_query(" SELECT xcart_products_categories.categoryid,xcart_categories.category,xcart_categories.parentid FROM xcart_products_categories, xcart_categories WHERE xcart_products_categories.productid = $productid AND xcart_categories.categoryid = xcart_products_categories.categoryid AND xcart_categories.parentid != 0");

while ($category = db_fetch_row($cat_result)) {
        if ($category) { $product_categories[] = $category; }
}

$smarty->assign("product_categories",$product_categories);


This selects all the categories that this product is in, unless they are parent categories, and assigns the array (where each row =array(category id, category name)) to Smarty as $product_categories.

In skin1/customer/main/product.tpl I added an include to call my new ugly "categories.tpl" which in its most basic (and quite ugly) form looks like this:

Code:

{section name=category loop=$product_categories}
        [img]{$ImagesDir}/categories/{$product_categories[category].0}.gif[/img]
{/section}


James.
xx

Hunabku 07-05-2006 11:37 PM

Categories for product (Cleaned up & with nesting)
 
I have cleaned up and added to jamesc code to include sub category nesting(indenting). I used a spacer.gif for indenting but you can substitute with unordered list, character space, etc.

In product.php around line 143 insert the following:

Code:

# This mod comes from xcart user forum - http://forum.x-cart.com/viewtopic.php?t=29077
# It puts a product's categories into an array so we can access them from the added template: customer/categories_for_selected_product.tpl.
# In the SELECT below [categories].categoryid_path contains the path or nesting of categoryid(s) ex. "256/262/270".
$cat_result = db_query(" SELECT $sql_tbl[products_categories].categoryid,$sql_tbl[categories].category,$sql_tbl[categories].parentid,$sql_tbl[categories].categoryid_path FROM $sql_tbl[products_categories], $sql_tbl[categories] WHERE $sql_tbl[products_categories].productid = $productid AND $sql_tbl[categories].categoryid = $sql_tbl[products_categories].categoryid AND $sql_tbl[categories].parentid != 0"); 
$i = 0;
while ($category = db_fetch_row($cat_result)) {
  if ($category) { $product_categories[] = $category;
  # The following two lines of code create the subcategory nesting(indent) values.
  # $category_path_chars gets an array of ascii character counts from categoryid_path.
  $category_path_chars = count_chars($product_categories[$i][3],1);
  # Replace the categoryid_path element with the number of times "/"(ascii char 47) occurs in categoryid_path.  Subtract 1 from this value and multiply by 12 (12px) for the pixel width of spacer.gif to indent each nested subcategory - see customer/categories_for_selected_product.tpl.
  $product_categories[$i][3] = ($category_path_chars['47']-1)*12;
  $i++; } }
$smarty->assign("product_categories",$product_categories);


Now create a new template in the customer directory and call it "categories_for_selected_product.tpl" - this is just a modified "categories.tpl" file from the same directory. Include the following code:

Code:

{* $Id: categories_for_selected_product.tpl based on categories.tpl,v 1.23 2004/06/24 09:53:29 max Exp $ *}
{*** See product.php for the creation of the $product_categories array used below. ***}
{capture name=menu}
{section name=category loop=$product_categories}
<FONT class="CategoriesList">
{if $product_categories[category].3 > 0}
{*** Do indenting ***}
[img]{$ImagesDir}/spacer.gif[/img]
{/if}
{$product_categories[category].1}</FONT>

{/section}
{/capture}
{ include file="menu.tpl" dingbats="dingbats_categorie.gif" menu_title=$lng.lbl_categories menu_content=$smarty.capture.menu }


Now just include "categories_for_selected_product.tpl" in your customer/main/product.tpl.

Jerrad 07-06-2006 04:35 AM

Thanks for this usefull mod!
Is it possible to see it somewehere in 'action'? :D

Hunabku 07-06-2006 11:18 AM

Quote:

Originally Posted by Jerrad
Thanks for this usefull mod!
Is it possible to see it somewehere in 'action'? :D


Well thanks Jerrad but perhaps not so useful - just yet. There is a category sorting step(s) that needs to be added.

In my test I put a sub-sub category into the very last sub category and it worked fine - it showed up where it should - at the end. Recently however when I entered a sub-sub category to a sub category in the middle of my list it also showed up at the end - since the defaut sort order for categories is the time they were added.

One of the things I like about this code so far is that it only searches the catagory table once and then works with the array. I have a feeling that inorder to have nested subs with proper ordering it will require multiple (looped) searches of categories table - unless there is a way to cleverly reorder the array - that might be some involved programming though.

Back to the drawing board.... Can anyone help?

PS: I'll give you the URL to my store as soon as this is cleared up.

Jerrad 07-06-2006 11:23 AM

I'm looking forward to your url, when you're finished at the drawingboard... :lol:
Thanks!

wendy.email 07-09-2006 02:52 PM

I think this is what I have been looking for... is that something like product advisor? I want to make something like the following link.

http://www.vitabase.com/tools/product-advisor.asp


All times are GMT -8. The time now is 01:42 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.