Question

I'm using the CakeDC search plugin. I have a simple shopping cart with products that all belong exclusively to subcategories. for example subcategories of food may be fruit, vegetables, meats, etc. so a pear only belongs to the fruit category.

So I have a dropdown that has all categories, if I select a parent category food, no items show up, if I select fruit, then the fruit will show up.

The behavior I want is to initially only show parent categories, so food, sporting goods, etc. then when they select food I want a few different things to happen:

1) show all products that are associated with this category as well as all the child categories

2) display another dropdown with fruit, vegetables, meat, etc. for further filtering

3) if there are subcategories to this subcategory, show their children as well until filtered more.

My code right now only allows me to find direct associations with a category

Here is the code from my model Product.php

 public $actsAs = array('Search.Searchable');

    public $filterArgs = array(
        array('name' => 'cid', 'field' => 'category_id', 'type' => 'value', 'method' => 'query', 'method' => 'query', 'allowEmpty' => true);


    public function filterCat($data, $field = null) {
        if (empty($data['cid'])) {
            return array();
        }
        $cat = $data['cid'];
        return array(
            'OR' => array(
                $this->alias . '.category_id' => $cat,
                ));
    }

I have searched high and low for a solution, which I would think would be simple but haven't found anything. Any help is highly appreciated!

Was it helpful?

Solution

I was able to achieve my desired functionality by modifying the filterCat() function as shown below.

public function filterCat($data, $field = null) {
    if (empty($data['cid'])) {
        return array();
    }
    $cat[] = $data['cid'];
    // Get child categories
    $children = $this->Category->children($cat['0'],false,'id');
    // extract the ids of the child categories
    $children = Hash::extract($children, '{n}.Category.id');
    // if no child categories, return requested category id
    if(empty($children)){
        return array(                
           $this->alias . '.category_id' => $cat);
    }else{
    // else merge array of child category ids with parent category id
    // and create SQL string to match the list of category ids
        $children = array_merge($cat,$children);
        return array(                
            $this->alias . '.category_id IN ' => $children);    
    }
}

The function looks for child categories, if there are no child categories it returns the search string with the category id. If there are child categories, it returns the search string with the parent and child category ids in a list.

I know this is an old question, but hopefully this can help others.

CakePHP version 2.2.3 and CakeDC search plugin version 2.1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top