X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   Showing only certain categories in the sidebar / adding a second categories box (https://forum.x-cart.com/showthread.php?t=78135)

bionuts 02-03-2021 05:20 AM

Showing only certain categories in the sidebar / adding a second categories box
 
Hi all,

We're in the process of upgrading our last website to 5.4.

On this site, we use two types of category: one to sort by product type and one to sort by product application. I therefore want to add a second categories sidebar to the site so that I can separate them.

Adding the custom module, etc, to display a second box in the sidebar is something that I've done on a previous site (although not to display a category list), so I think I can cover that bit. The bit I am struggling with is how I formulate the code to select which categories to display in which box.

When I did this on a previous version of X-Cart, I was able to do it by using an IF statement and looking at the $category_id (as shown here https://forum.x-cart.com/showpost.php?p=280361&postcount=13) but I'm struggling with it on X-Cart 5.

Does anyone know how I can code an IF statement to select (for instance) only categories with an ID of less than 50? Or have any suggestions of a better way to achieve what I'm trying to do?

Thanks in advance.

cflsystems 02-03-2021 05:49 AM

Re: Showing only certain categories in the sidebar / adding a second categories box
 
I wouldn't count on the category id as you may add new categories or remove existing or rename existing to move them form one to the other.
I see 2 ways of doing that - using the category position or adding new property exclusively for this purpose.
If you use category position you can select categories with position less than X for the first block and the rest for the second block (or position more than X for one block and the rest for the other, whatever works best). The problem with using position is that categories are sorted by position to begin with and you will have to be very careful how you manage position so it satisfies both.
I think it is better to add another filed to the categories table for this purpose. Whatever you call it doesn't matter and it can just be boolean field - true being type and false being application. Then you can do the queries to select categories for type and application based on this field's value.

bionuts 02-03-2021 11:03 AM

Re: Showing only certain categories in the sidebar / adding a second categories box
 
Hi Steve,

Thanks for your reply.

Yes, using the category_id isn't ideal, but I had experience doing it that way from a previous version of X-Cart.

I think I would prefer to use position, rather than trying to add new fields to the database and I could envisage that product type could be in one number range and application could be in a different range. That way, they'd always be separated, even if the positions were changed.

I've managed to work out the IF statement syntax I would need if i use category_id, but I can't seem to find the correct name / syntax to reference for category position. Any ideas?

Thanks in advance.

Ed B. 02-03-2021 11:39 AM

Re: Showing only certain categories in the sidebar / adding a second categories box
 
The table xc_categories have two fields called rpos and lpos, which, I think, correspond to"category positions".

cflsystems 02-03-2021 03:28 PM

Re: Showing only certain categories in the sidebar / adding a second categories box
 
Quote:

Originally Posted by bionuts
I've managed to work out the IF statement syntax I would need if i use category_id, but I can't seem to find the correct name / syntax to reference for category position. Any ideas?





Using he position may confuse you because categories are positioned within their parent category. So the position field may hold same value for 1st level category and 3 level category. This will only work if you have one level of categories or if you do the splitting based on 1st level categories only.


The category position field in the table is called "pos". "lpos" and "rpos" are used to position the category in relation to prev and next and parent category.

bionuts 02-04-2021 12:05 PM

Re: Showing only certain categories in the sidebar / adding a second categories box
 
Hi,

Yes, all the categories are at root level.

If I use a statement such as:

Code:

{% if this._category.id < '40' %} [where category ids below 40 are the ones I want to show]
<p>a test to see if this works</p>
{% endif %}


then I can get the result I'm looking for. If I use

Code:

{% if this._pos < '200' %} [where categories with a position of less than 200 are the ones I want to show]
<p>a test to see if this works</p>
{% endif %}


then it doesn't work.

As most of my programming experience stems from Sinclair Basic waaaay back in the day, I think it's probably a lack of understanding of the correct syntax to use, so any tips are gratefully received.

cflsystems 02-04-2021 01:00 PM

Re: Showing only certain categories in the sidebar / adding a second categories box
 
"this" does not refer to a category so there is no "pos" for it.
I am not sure where you are using this "this._category.id" so hard to say what you have there.

In this case you can use "this._category.pos" or "this._category.position" - depends on how it is called within the _category array. You can also try "this._category.getPos()" or "this._category.getPosition()". If none of them works you will have to decorate the appropriate class behind the template and see how to add the category position to each element so you can use it in the template.

bionuts 02-04-2021 01:50 PM

Re: Showing only certain categories in the sidebar / adding a second categories box
 
I think I'm getting too far down the rabbit hole on this one for the knowledge that I have (and don't currently have the spare time to do the necessary learning for something that is obviously more involved than I was hoping it would be).

So I think I'll run with using category.id, as I can get that working at present and worry about possible issues that new categories might present when they occur. By then, I might have had time to learn what I'm doing. :-)

Many thanks for your help, Steve.


All times are GMT -8. The time now is 11:17 PM.

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