Jump to content
cboetens

How to create product filters?

Recommended Posts

Hi guys

I guess this is the right forum to post my question. If not, do let me know. Ok, I'm fairly new to ProcessWire and so far I really like the CMS! It's magnificent to use and really straight forward. Big thanks to Ryan for that.

Now, the question I have. I have a template called 'products_categories.php' in which I collect and render all the different categories (the categories basically act as a filter) for a variety of products and next to the categories, I render all the products, like so:

<?php include('includes/header.php') ?>
<?php
$categoryName = $sanitizer->pageName($input->urlSegment(1));
$subcategoryName = $sanitizer->pageName($input->urlSegment(2));

pre($categoryName);
pre($subcategoryName);

$categories = $page->get("template=products_categories");
$category = $categories->get("template=products_categories_item, name=" . $categoryName);

if($category->id){
    $session->category = $category->id;
    $subcategory = $category->get("template=products_categories_item, name=" . $subcategoryName);
    
    if($subcategory->id){
        $session->subcategory = $subcategory->id;
    }
}

$selector = "template=products_item";
if($category->id){
    $selector .= ", products_categories=" . $category;
}

if(isset($subcategory->id)){
    $selector .= ", products_categories=" . $subcategory;
}

$products_items = $page->find($selector);

?>
<div class="uk-section">
    <div class="uk-container">
        <?php if(!empty($page->headline)) { ?>
        <h1><?= $page->headline ?></h1>
        <?php } ?>
        
        
        <!-- **** COMMENT: create grid for products **** --->
        <div class="row">
            
            <div class="col-md-3">
                <div class="uk-card mr-3 uk-card-default uk-card-body">
                    <h3 class="uk-card-title">Productcategorieën</h3>
                    <ul class="uk-list">
                        <?php
                        foreach($categories->children() as $c){
                        ?>
                        <li class="<?= ($category->id===$c->id ? 'active ' : '') ?>">
                            <a href="<?= $page->url . $c->name . '/' ?>"><?= $c->title; ?></a>
                            <?php if($c->hasChildren){ ?>
                            <ul>
                                <?php foreach($c->children() as $sc) {?>
                                <li class="<?= ($category->id==$c->id && $subcategory->id==$sc->id ? 'active ' : '') ?>">
                                    <a href="<?= $page->url . $c->name . '/' . $sc->name . '/' ?>"><?= $sc->title; ?></a>
                                </li>
                                <?php } ?>
                            </ul>
                            <?php } ?>
                        </li>
                        <?php
                        }
                        ?>
                    </ul>
                </div>
            </div>
            
            <div class="col-md-9">
                <div class="uk-child-width-1-3@s uk-grid-match uk-grid-margin-small uk-grid-small" uk-grid>
                    <?php
                    foreach($products_items as $product) {
                    ?>
                    
                    <div class="uk-card">
                        <?php if(isset($product->image)) { ?>
                        <div class="uk-card-media-top">
                            <img src="<?= $product->image->URL; ?>" title="<?= $product->title; ?>" alt="<?= $product->intro; ?>">
                        </div>
                        <?php } ?>
                        <?php if(!empty($product->title) || !empty($product->intro)) {  ?>
                        <div class="uk-card-body uk-card-default">
                            <?php if(!empty($product->title)) { ?>
                            <h3 class="uk-card-title"><?= $product->title; ?></h3>
                            <?php } ?>
                            <?php if(!empty($product->intro)) { ?>
                            <p><?= $product->intro; ?></p>
                            <?php } ?>
                            <?php if (!empty($product->price)): ?>
                            <h3 class="uk-card-title">&euro;&nbsp;<?= $product->price; ?>&nbsp;(excl. btw)</h3>
                            <?php endif; ?>
                            <a class="uk-button uk-button-primary" href="<?= $product->url; ?>">Bekijk</a>
                        </div>
                        <?php } ?>
                    </div>
                    
                    
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include('includes/footer.php'); ?>

Now, I can't seem to get the filter to work. The URL behind every (sub)category should go straight back to the template 'products_categories.php', that way I can get a range of products according to the selected/ clicked URL.

What am I missing here? Is this not the correct way to handle things?

Any help is welcome!

Thanks.

Cédric

Share this post


Link to post
Share on other sites

Hello and welcome to the forum!

Glad you figured it out. Would be nice if you could also share what the problem was, so that others can learn from it.

Was it this line?

1 hour ago, cboetens said:

$products_items = $page->find($selector);

 

Share this post


Link to post
Share on other sites
3 minutes ago, gebeer said:

Hello and welcome to the forum!

Glad you figured it out. Would be nice if you could also share what the problem was, so that others can learn from it.

Was it this line?

 

Hi Gebeer!

Thanks for the kind words! Indirectly that line indeed caused the problem.

If you have a look at the two if-statements just above this line, you will see that I don't check if the values "$category->id" and "$subcategory->id" are set or not and if they are empty or not. With the faulty code the variable "$selector" in the line of code that you mentioned above is empty.

That way, the products are not being loaded when a category is selected. This was problem 1.

I've made a second adjustment. In my code I have renamed the template "products_categories.php" to "products.php". As an alternate template name for "products_categories.php" I have set "products.php". I don't really know if an alternate file name is necessary tough?

That way I can easily grab a category and select all products linking to that category, I guess?

The code that resolved my problem was the following:

if(isset($category->id) && !empty($category->id)){
    $selector .= ", products_categories=" . $category;
}

if(isset($subcategory->id) && !empty($subcategory->id)){
    $selector .= ", products_categories=" . $subcategory;
}

Feel free to post any feedback. It's an interesting subject.

Greetings.

  • Like 2

Share this post


Link to post
Share on other sites

Thanks for posting the solution to your problem!

16 minutes ago, cboetens said:

if(isset($category->id) && !empty($category->id)){

To test if a full page object was returned by $pages->get() you can use
 

if($category->id) {...}

This is usually enough. When PW doesn't find a page it returns an object NullPage. This NullPage object returns null for id. So the simple test above is sufficient.

Since you get your category and subcategory names from the urlSegments, you could test earlier in your code if these exist. If there is no urlSegment(1),

$categoryName = $sanitizer->pageName($input->urlSegment(1));

will return an empty string. So you could test for that.

I can recommend to start early on using the fabulous debugger module TracyDebugger. It makes dev life easier and can save a lot of time. I use it on every project. Also it is always goo to have the API reference open in a separate tab or browser window 🙂

  • Like 3

Share this post


Link to post
Share on other sites
13 minutes ago, gebeer said:

Thanks for posting the solution to your problem!

To test if a full page object was returned by $pages->get() you can use
 


if($category->id) {...}

This is usually enough. When PW doesn't find a page it returns an object NullPage. This NullPage object returns null for id. So the simple test above is sufficient.

Since you get your category and subcategory names from the urlSegments, you could test earlier in your code if these exist. If there is no urlSegment(1),


$categoryName = $sanitizer->pageName($input->urlSegment(1));

will return an empty string. So you could test for that.

I can recommend to start early on using the fabulous debugger module TracyDebugger. It makes dev life easier and can save a lot of time. I use it on every project. Also it is always goo to have the API reference open in a separate tab or browser window 🙂

Hi gebeer

Thanks for the excellent suggestions! I am going to test these out immediately.

In the meanwhile I've adjusted both of the if-statements. It's the little tips that make a lot of difference to me, thanks for that!

As I'm gaining more and more experience, I am sure I will discover a lot of other small tips.

Thanks again!

  • Like 1

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benbyf
      Hello, and welcome to what I though was either my client being silly and changing things, or some evil doer. Turns out its reproducible and therefore something in Proceswire (I checked my templates and modules but couldnt find anything that would be doing this...). So what is it doing? Check out the video for evidence.
      A repeater field is interacting with a page template and another repeater field somehow to swap the fields in the template and repeater over...
      I have a template called team, and a repeater field called team_repeater with label Team. Some how and for some reason, when I change my fields on repeater called main_menu_links my team template gets those fields and when I try and revert the team template fields to the fields it should have, they get given to the repeater main_menu_links. Also this to say HELP!!!!!
      video: https://www.dropbox.com/s/exkdhc6n7x0xpsa/strange-repeater-PW-mega-bug.mov?dl=0
    • By Aleksey Popov
      Hi! Is it possible to specify explicitly the parent section when creating a new child page with a single template?
      The idea is to save the user from the possibility of mistakenly choosing a section, but not to create unnecessary identical templates
      What I mean:
      Let's say I've got 2 parent categories with names articles & news. Each of them utilized the same admin-template (category) with the category.php file. Also, I have 2 different templates for child pages (article & news).
      Next, I need to add an article page to the articles category with the "create new" button in admin. At that moment, I must choose the category to place (because both categories use the same template). 
      To runaround this (and create a page right into the exact category without the choosing step) I should create 2 templates for each category, and setup parent-children relations for each pair of templates, right?
      But Is any way to use only the one category template and different children templates and at the same time, explicitly specify which section to use for child pages, thereby removing the process of selecting a child section?
       
      articles (category.php) Article page 1 (page-article.php) Article page 2 (page-article.php) Article page 3 (page-article.php) + Add a new article page here without category chooser news (category.php) News page 1 (page-news.php) News page 2 (page-news.php) News page 3 (page-news.php)  Update: found module  but I can't get it work.
       
    • By fruid
      Hi,
      this is the first time I'm using ProcessWire.
      I thought I get how fields, template and pages work, but when I create a template in the CMS, it doesn't generate any file in site/templates/
      Then I thought I might need to create a blank file myself manually on the FTP (which already seems odd to me).
      Once I did that, I tried to add fields to the template but again, doesn't write to the php file.
      When I create a new page and apply said template to it, the page stay blank.
      AFAIK the mod_rewrite of the apache is on and I went for the worst case scenario described here https://processwire.com/docs/security/file-permissions/ and set all file-permissions for future files to 0666 and folders to 0777 in the config.php
      What am I not getting and what am I doing wrong?
      Help is appreciated, stay save everybody,
      Fred
    • By DooM
      Hello,
      I'm working on something like "structure generator" for my own needs which is run after clean installation of PW to create basic structure, fields, install modules, remove unnecessities, etc.
      I'm trying to remove site/templates/scripts and site/templates/styles folders, but I'm out of ideas already. When I try PHP's unlink function, it says that the path doesn't exists, which is obvious, because it will try to delete those folders under FileCompiler folder. This is probably because PW compilation process? I'm initializing this mine "structure generator" in ready.php.
      Do any of you guys have any idea how to get rid of those folders? My styles and scripts folders are separated, so I don't need those two inside templates folder.
      Thanks for every advice. 🙂
    • By MateThemes
      Hello everyone!
      I am trying to add my repeater matrix fields to the search selector, but unfortunately nothing seems to work for me.
      I have following search code in my search.php:
      <?php namespace ProcessWire; // look for a GET variable named 'q' and sanitize it $q = input()->get('q'); // sanitize to text, which removes markup, newlines, too long, etc. $q = sanitizer()->text($q); // did $q have anything in it after sanitizing to text? if($q) { // Make the search query appear in the top-right search box. // Always entity encode any user input that also gets output echo '<input id="search-query" value="' . sanitizer()->entities($q) . '">'; // Sanitize for placement within a selector string. This is important for any // values that you plan to bundle in a selector string like we are doing here. // It quotes them when necessary, and removes characters that might cause issues. $q = sanitizer()->selectorValue($q); // Search the title and body fields for our query text. // Limit the results to 50 pages. The has_parent!=2 excludes irrelevant admin // pages from the search, for when an admin user performs a search. $selector = "title|body~=$q, limit=50, has_parent!=2"; // Find pages that match the selector $matches = pages()->find($selector); } else { $matches = array(); } // unset the variable that we no longer need, since it can contain user input unset($q); ?> <main pw-replace='main'> <?php include('./includes/_pageheadersearch.php'); ?> <div id='content-body' class='uk-section uk-section-large uk-section-large'> <div class='uk-container uk-container-small'> <?php // did we find any matches? if(count($matches)) { // yes we did, render them echo ukAlert(sprintf(_n('Found %d page', 'Found %d pages', $matches->count), $matches->count), "default", "check"); echo ukDescriptionListPages($matches); } else { // we didn't find any echo ukAlert(__('Sorry, no results were found'), "danger", "warning"); } ?> </div> </div> </main> I have tried to add my fields to the selector code (repeater_matrix.aboutsblock_repeaters.mytextfield) . But I didn't get any results.
      What I am doing wrong?
      Thanks for your help!
×
×
  • Create New...