Missing ajax dropdown category logic

Posted in: Newspaper
Post count: 63

Currently, adding an Ajax dropdown menu to a block based on categories will not take into account the main block category filter. An example:


Cars
- BMW
- Audi

Colors:
- Red
- Blue

Block Category filter: "BMW"

Ajax filter: "filter by categories"
Ajax dropdown: "show the following IDs": Red, Blue

Now selecting the “Red”, “Blue” from the Block Ajax filter, will also show every “Red” and “Blue” Audi . Is there not a way to apply some needed logic to the functions or make the ajax dropdown respect the block category filter:

Show ajax category where Colors “Red” and “Blue” AND Cars = “BMW”.

Without the logic I’ll have to triple categories and even have multiple sub categories with the same name (not ID).

Can you plese offer a solution or at least point to the specific functions (either the framework og the jquery scripts) needed to make the modifications?

  • This topic was modified 10 years by Phonetic.
  • This topic was modified 10 years by Phonetic.
Post count: 23312

Hello Phonetic,

Unfortunately, these types of filters have not been designed to be used in this case, as you wish and unfortunately, I cannot give you a quick response for your request because it is not a simple task to implement this. I have added your suggestion to our list of improvements and in a future update it is possible but we cannot say for sure if it will get done in the next update because we have a big list of fixes, improvements, and suggestions to implement. It’s hard to filter them and select which one gets implemented now and which one gets postponed. The developers decide if it gets done or not, though. Plese notice that is not a simple task and easy to implement this. If you want to display more levels in our theme and you need to have this option as soon as possible, please consider hiring a freelancer/developer to implement this in our theme because we cannot provide customization services at the moment, sorry!

Thank you for your understanding!

Post count: 63

Hi Catalin.

I’m a developer myself. Can you point me to which part of the framework I need to take a look at?

The specific function is in td_ajax.php:case 'td_category_ids_filter': // by category - the user selected a category from the drop down. if it's empty, we show the default block atts
if (!empty($ajax_parameters['td_filter_value'])) {
$ajax_parameters['td_atts']['category_ids'] = $ajax_parameters['td_filter_value'];
unset($ajax_parameters['td_atts']['category_id']);
}
break;

  • This reply was modified 10 years by Phonetic.
  • This reply was modified 10 years by Phonetic.
Post count: 22421

Hi,
Every block in particular is an extended class of td_block.php. That is the file where you will find the render function: http://screencast.com/t/stAjkHJMUr This function will process the information stored in td_date source:
http://screencast.com/t/0RfkytDf
Thank you!

Post count: 63

Hi Bogdan.

I’ve implemented the changes to the backend and added a new option ‘Filter by categories and block filter’ for blocks (td_config.php)


    static function get_map_block_ajax_filter_array() {
        return array(
            //custom filter types
            array(
                "param_name" => "td_ajax_filter_type", //this is used to build the filter list (for example a list of categories from the id-s bellow)
                "type" => "dropdown",
                "value" => array('- No drop down ajax filter -' => '', 'Filter by categories' => 'td_category_ids_filter', <strong>'Filter by categories and block filter' => 'td_category_ids_parent_filter'</strong>, 'Filter by authors' => 'td_author_ids_filter', 'Filter by tag slug' => 'td_tag_slug_filter', 'Filter by popularity (Featured | All time popular)' => 'td_popularity_filter_fa'),
                "heading" => 'Ajax dropdown - filter type:',
                "description" => "Show the ajax drop down filter. The ajax filters (except by popularity) require an additional parameter. If no ids are provided in the input below, the filter will show all the available items (ex: all authors, all categories etc..)",
                "holder" => "div",
                "class" => "",
                "group" => "Ajax filter"
            ),

I’ve added an new case to td_block.php:


                    case 'td_category_ids_parent_filter': // by category AND block / menu category filter
                        $td_categories = get_categories(array(
                            'include' => $td_ajax_filter_ids,
                            'exclude' => '1',
                            'number' => 100 //limit the number of categories shown in the drop down
                        ));

                        // check if there's any id in the list
                        if (!empty($td_ajax_filter_ids)) {
                            // break the categories string
                            $td_ajax_filter_ids = explode(',', $td_ajax_filter_ids);

                            // order the categories - match the order set in the block settings
                            foreach ($td_ajax_filter_ids as $td_category_id) {
                                $td_category_id = trim($td_category_id);

                                foreach ($td_categories as $td_category) {

                                    // retrieve the category
                                    if ($td_category_id == $td_category->cat_ID) {
                                        $td_pull_down_items [] = array(
                                            'name' => $td_category->name,
                                            'id' => $td_category->cat_ID,
                                        );
                                        break;
                                    }
                                }
                            }

                        // if no category ids are added
                        } else {
                            foreach ($td_categories as $td_category) {
                                $td_pull_down_items [] = array(
                                    'name' => $td_category->name,
                                    'id' => $td_category->cat_ID,
                                );
                            }
                        }
                        break;

I could have done it the lazy way:


case 'td_category_ids_filter' && 'td_category_ids_parent_filter':

But might as well add a proper case.

I’ve also modified td_ajax.php to handle category ids as an array (this is needed for td_data_source.php – see below):


				case 'td_category_ids_parent_filter': // by category AND block main category  - the user selected a category from the drop down. if it's empty, we show the default block atts

					if (!empty($ajax_parameters['td_filter_value'])) {
						$ajax_parameters['td_atts']['category_ids'] = array($ajax_parameters['td_atts']['category_id'], $ajax_parameters['td_filter_value']);
						unset($ajax_parameters['td_atts']['category_id']);	
					}

					// debug
					// $output = print_r($ajax_parameters['td_atts'], true);
					// file_put_contents('ajax_query.txt', $output);					

					break;

Finally changes to td_data_source.php:


        // if $category_ids = array then only get posts with main block / menu category AND ajax filter set
        // if $category_ids is an aray then prepare query with "category_and" instead of "cat"    
        if (!empty($category_ids) and is_array($category_ids)) {
            $wp_query_args['category__and'] = $category_ids;
        }

Checks for $category_ids == array and then prepares query with “category__and” instead of “cat”.

Changes enables you so choose the function with the main block / menu category id as parent for data in sub tabs.

Let me know if you guys want the diffs from code. I’m pretty sure a lot of your customers would love this imho much needed added feature.

Disregard the out-commented debug code.

Cheers 🙂

  • This reply was modified 10 years by Phonetic.
Post count: 22421

Hi,
Thank you for posting this solution and congrats for getting it working. If you feel like sharing it with the community to help our users, you can post a “guide” for it in a new topic and I will add it to the best of forums section so people can benefit from it.
Thank you!

Post count: 63

You’re welcome Bogdan.

Forgot to remove the code indentation. code quotes are a bit messy 🙂

I’ll write a guide post when I got some spare time. Launch is in less than two weeks 🙂

Post count: 22421

Best of luck on your project!

Post count: 4

I was reading your post and it’s amazing, i don’t know if you could share the guide for achieve this, i’m looking for something familiar i have a session set in my header and the only think i want is that all the post use category__and for create a division. I hope you can help me, thanks in advance.

Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic.