Forum

Thread tagged as: Question, Problem, Field-Types

User Filtering Content combined with Categories

Hi, I'm wondering how to get categories filtered like regular content?

I'm using the basis as your "How do I create a user-filterable list - https://solutions.grabaperch.com/architecture/user-filtered-lists" tutorial.

Page:

<?php

    // Create an empty array ready to add our filters to
    $filters = array();

    $varOnSpecial = (perch_get('On-Special'));
    if ($varOnSpecial == 'true') {  
        if(perch_get('On-Special')) {           
            $filters[]  =   array(
                'filter'    =>      'Sale-Price',
                'match'     =>      'gte',
                'value'     =>      '1',                        
            );                                              
        }
    }   

    $varIndoorOrOutdoor = (perch_get('Indoor-or-Outdoor'));
    if ($varIndoorOrOutdoor != 'Indoor or Outdoor') {
        if(perch_get('Indoor-or-Outdoor')) {            
            $filters[]  =   array(
                'filter'    =>      'Indoor-or-Outdoor',
                'match'     =>      'eq',
                'value'     =>      perch_get('Indoor-or-Outdoor'),                     
            );          
        } 
    }

    /* STUCK AREA NOT WORKING */
    $varBrand = (perch_get('Brand'));
    echo $varBrand;
    if ($varBrand != 'Brands') {                    
        if(perch_get('Brand')) {            
            $filters[]  =   array(
                'set'=>'Brands',
                'filter'=> 'catTitle',
                'match' => 'eq',
                'value' => perch_get('Brand'),
                'skip-template' => true

            );                          
        }
    }       
    /* END STUCK AREA */            

    $varLeadTime = (perch_get('Lead-Time'));
    if ($varLeadTime != 'Lead Time') {                  
        if(perch_get('Lead-Time')) {            
            $filters[]  =   array(
                'filter'    =>      'Lead-Time',
                'match'     =>      'eq',
                'value'     =>      perch_get('Lead-Time'),                     
            );                                              
        }
    }

    // Unset the filters if none are used:
    if (!count($filters)) $filters=false;                   

    // Then get the list
    perch_content_custom('Product-Items', array(
        'template'      =>      'product_item_summary.html',
        'sort'          =>      'Regular-Price',
        'sort-order'    =>      'ASC',
        'filter'        =>      $filters,             
    ));

?>

Form:

<perch:form id="filter" method="get">
    <p class="on-special">
        On Special
        <perch:input type="checkbox" id="On-Special" value="true" />
    </p>
                <perch:input type="select" id="Indoor-or-Outdoor" options="Indoor or Outdoor, Indoor, Outdoor" />
                <perch:input type="select" id="Brand"
    options="Brands,
    MIDJ,
    Ego Paris,
    Grosfillex,
    XO" />
    <perch:input type="select" id="Lead-Time" options="Lead Time, In-Stock, 0-4 Weeks, 4-12 Weeks" />
    <perch:input type="submit"   value="Filter" />
</perch:form>

Categories:

Title: Brands, Slug: brands

Category: Ego Paris, Path: brands/ego-paris/ | Category: Grosfillex, Path: brands/grosfillex/ | Category: MIDJ, Path: brands/midj/ etc.

Set Template: Set

<perch:categories id="setTitle" type="smarttext" label="Title" required="true" />
<perch:categories id="setSlug" type="slug" for="setTitle" label="Slug" />

Category template: Category Logo

<perch:category id="catTitle" type="smarttext" label="Title" required="true" />
<perch:category id="catSlug" type="slug" for="catTitle" suppress="true" />
<img src="<perch:category id="image" type="image" label="Logo" />" />

Template:

(Master) product_item.html

<perch:content id="Product-Title" type="text" label="Product Title" required="true" title="true" />
<perch:content id="slug" for="Product-Title" type="slug" suppress="true" />

<perch:categories id="Brand" label="Brand" set="brands">
    <perch:category id="image" type="image" label="Logo" />
    <perch:category id="brandTitle" type="smarttext" label="Title" required="true" />
</perch:categories>

<perch:content id="Regular-Price" type="text" label="Regular Price" />

<perch:content id="Sale-Price" type="text" label="Sale Price" />

<perch:content id="Lead-Time" type="select" label="Lead Time" options="In-Stock, 0-4 Weeks, 4-12 Weeks" allowempty="false" />

<perch:content id="Indoor-or-Outdoor" type="select" label="Indoor or Outdoor" options="Indoor, Outdoor" allowempty="false" />

etc.

Would be much appreciated for anyone who can solve the above!

I also have some related questions if not too much bother:

  1. Can the form drop down options be based on categories/title within set? I.e. as new brands are added to the category set, the field drop down for brands is already up to date.

  2. Can the filters be submitted without having to click the submit button? I.e. the page is updated (form submitted) on changing of the select box drop downs or checkbox.

  3. Is it possible (and is there a tutorial) for sorting the order of the results by e.g. A - Z, Z - A, Price Ascending, Price (descending) etc.

Cheers

James Hazelton

James Hazelton 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you wanting to filter categories, or filter content by category?

Filter content by category.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use the category option for that.

perch_content_custom('Product-Items', [
    'category' => 'products/clothing/trousers',
]);

Thanks this works for one, however I need to combine category filtering for AND, rather than the default OR.

                        if(perch_get('Designer') != 'Designer') {           
                            $varDesigner = perch_get('Designer');
                        } else {
                            $varDesigner = NULL;
                        }

                        if(perch_get('Brand') != 'Brand') {         
                            $varBrand = perch_get('Brand');
                        } else {
                            $varBrand = NULL;
                        }

                        if ($varBrand && $varDesigner) {
                            $varCombinedCategories = array('designers/'.$varDesigner, 'brands/'.$varBrand);
                        } else if ($varDesigner && !$varBrand) {
                            $varCombinedCategories = array('designers/'.$varDesigner);
                        } else if ($varBrand && !$varDesigner) {
                            $varCombinedCategories = array('brands/'.$varBrand);
                        } else {
                            $varCombinedCategories = '';
                        }


                        // Then get the list
                        perch_content_custom('Product-Items', array(
                            'template'      =>      'product_item_summary.html',
                            'page'          =>      '/products/*',      
                            'filter'        =>      $filters,
                            'category'      =>      $varCombinedCategories,
                        ));          

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can set category-match to any or all.

Works, thanks!

Is it possible to filter categories? For example something like this:

perch_categories(array(
        'set' => 'products',
        'template' => 'category-thmb-list.html',
        'filter'=>'home_item',
                'match'=>'eq',
                'value'=>'home',
        'count' => 4,
        )
    );