JoshoB Posted March 20, 2019 Share Posted March 20, 2019 So I have another problem. On one of our websites, we sell educational books. Each book is assigned to one or more categories. Yesterday, I added subcategories and assigned books to them. The structure for the categories are as follows: Home Categories (template: categories.php) Category (template: category.php) Subcategory (template: category.php) Books are assigned categories via a PageArray field (with asmSelect) also called "categories". Books themselves have a template "product.php" and are grouped under "Products" (with template "product.php"). To make sure books are listed in the proper category on the website, I used this selector: $selector = "template=product, limit=10, categories.parent=$page, sort=$sort_results"; $sort_results is a simple variable to switch between alphabetical sort, sort by release date, etc. The issue is the categories.parent=$page element. To my mind, this should list products that have a category that is either the requested page (people are browsing the "Latin" category), as well as all subcategories. The problem is that products in subcategories are ignored by ProcessWire. So if a book is listed in "Latin" and another has a category that's a child of "Latin", the first book is shown when I'm browsing the "Latin" category, but not the second one. The second one is shown ony if I go to the subcategory page. Does someone have a solution for this issue? I just want the category page to list all products in that category including subcategories. Thank you! Link to comment Share on other sites More sharing options...
Tom. Posted March 20, 2019 Share Posted March 20, 2019 Hi @JoshoB, Trying my hardest to understand this - from what I can tell, `$page` is category.php template? I believe you need: $selector = "template=product, limit=10, categories=[has_parent|id={$page->id}], sort=$sort_results"; Link to comment Share on other sites More sharing options...
JoshoB Posted March 20, 2019 Author Share Posted March 20, 2019 8 minutes ago, Tom. said: Trying my hardest to understand this - from what I can tell, `$page` is category.php template? Ah yeah, sorry. That is correct: $page in this instance has a category.php template. Your solution sadly generated an error. I edited it to read: "template=product, limit=10, categories=[has_parent|id=".$page->id."], sort=categories.sort, sort=$sort_results" But then it doesn't list any products. I think that's because it now specifies it has to either have a parent or the ID needs to be specifically that of the page. What I'd like to do is to specify that the categories field includes either the current $page or one of the current page's children (i.e. the subcategories). So I was hoping something like this might work: categories.parent=$page|$page->children But no dice. Link to comment Share on other sites More sharing options...
Tom. Posted March 20, 2019 Share Posted March 20, 2019 4 minutes ago, JoshoB said: Ah yeah, sorry. That is correct: $page in this instance has a category.php template. Your solution sadly generated an error. I edited it to read: "template=product, limit=10, categories=[has_parent|id=".$page->id."], sort=categories.sort, sort=$sort_results" But then it doesn't show anything. Ah strange usually if you have double quotes using { } is fine to escape PHP. Unsure to why this isn't working - maybe has_parent only works on an Page object not ID. Does the following work for you? "template=product, limit=10, (categories=$page), (categories.has_parent=$page), sort=categories.sort, sort=$sort_results" Link to comment Share on other sites More sharing options...
JoshoB Posted March 20, 2019 Author Share Posted March 20, 2019 Weird, it generates an error: Error: Exception: Unknown Selector operator: '' -- was your selector value properly escaped? has_parent is apparently not valid as a subfield selector. Link to comment Share on other sites More sharing options...
dragan Posted March 20, 2019 Share Posted March 20, 2019 did you try parent.parent? Link to comment Share on other sites More sharing options...
Autofahrn Posted March 20, 2019 Share Posted March 20, 2019 Shouldn't this work with: $selector = "template=product, limit=10, categories={$page}|{$page->children}, sort=$sort_results"; Given that $page is either a category or subcategory then $page->children is either the list of subcategory pages or empty. Maybe the or operator must only exist when children exist (not tested). Link to comment Share on other sites More sharing options...
JoshoB Posted March 20, 2019 Author Share Posted March 20, 2019 26 minutes ago, Autofahrn said: Shouldn't this work with: $selector = "template=product, limit=10, categories={$page}|{$page->children}, sort=$sort_results"; Given that $page is either a category or subcategory then $page->children is either the list of subcategory pages or empty. Maybe the or operator must only exist when children exist (not tested). This looks like it ought to work! Sadly, it doesn't. $page->children doesn't seem to do anything (the results get dropped). Link to comment Share on other sites More sharing options...
JoshoB Posted March 20, 2019 Author Share Posted March 20, 2019 52 minutes ago, dragan said: did you try parent.parent? Like categories.parent.parent? That isn't valid. Link to comment Share on other sites More sharing options...
Autofahrn Posted March 20, 2019 Share Posted March 20, 2019 3 minutes ago, JoshoB said: $page->children doesn't seem to do anything (the results get dropped). So what is $page actually? Is it a category or subcategory (which does not have children)? As I pointed out, the or operator only must be specified when on a category. You may need to code like this: $pageSelect = "$page"; // page ID as a string if($page->hasChildren()) $pageSelect .= "|{$page->children}"; $selector = "template=product, limit=10, categories=$pageSelect, sort=$sort_results"; So $pageSelect would either look like "1234", if $page refers to a subcategory or "1456|1701|1702|1703" if its a category. 2 Link to comment Share on other sites More sharing options...
JoshoB Posted March 20, 2019 Author Share Posted March 20, 2019 Cheers @Autofahrn, that put me on the right track. {$page->children} didn't work as expected, but in the PHP file in question, I did loop through all categories to generate a clickable list, so I simply populated $pageSelect with the ID of every item as it looped through them. I then fed the variable into the selector and that did the trick! Thanks again for the help, everyone. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now