Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

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

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 02-03-2021, 05:20 AM
 
bionuts bionuts is offline
 

Senior Member
  
Join Date: Oct 2008
Posts: 109
 

Default 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.
__________________
Four sites using X-Cart 5.4.x.x. Oh the joy
Reply With Quote
  #2  
Old 02-03-2021, 05:49 AM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,197
 

Default 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.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #3  
Old 02-03-2021, 11:03 AM
 
bionuts bionuts is offline
 

Senior Member
  
Join Date: Oct 2008
Posts: 109
 

Default 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.
__________________
Four sites using X-Cart 5.4.x.x. Oh the joy
Reply With Quote
  #4  
Old 02-03-2021, 11:39 AM
 
Ed B. Ed B. is offline
 

X-Adept
  
Join Date: Apr 2016
Posts: 446
 

Default 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".
__________________
X-cart 5.2.12, php 5.6
Ed from Grenoble, France
Reply With Quote
  #5  
Old 02-03-2021, 03:28 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,197
 

Default 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.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following user thanks cflsystems for this useful post:
Ed B. (02-04-2021)
  #6  
Old 02-04-2021, 12:05 PM
 
bionuts bionuts is offline
 

Senior Member
  
Join Date: Oct 2008
Posts: 109
 

Default 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.
__________________
Four sites using X-Cart 5.4.x.x. Oh the joy
Reply With Quote
  #7  
Old 02-04-2021, 01:00 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,197
 

Default 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.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #8  
Old 02-04-2021, 01:50 PM
 
bionuts bionuts is offline
 

Senior Member
  
Join Date: Oct 2008
Posts: 109
 

Default 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.
__________________
Four sites using X-Cart 5.4.x.x. Oh the joy
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 05:06 AM.

   

 
X-Cart forums © 2001-2020