Filter Option for Unchecked Child Category by default on Selection Parent

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Filter Option for Unchecked Child Category by default on Selection Parent

This topic contains 14 replies, has 2 voices, and was last updated by theonlyit59 theonlyit59 3 years, 1 month ago.

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #31843
    theonlyit59
    theonlyit59
    Participant

    Hi,
    We are looking for option if we click parent category in filter, all child categories should be unchecked by default? How do we achieve this? Please see attachment for reference.

    Thank you

    Regards,
    Shayan Ahmed

    Attachments:
    You must be logged in to view attached files.
    #31858
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Do you mean like only opening the sub-categories, but not checking them?
    I’m afraid there is no option to do that unfortunately, but it might be possible via a custom code though.

    Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_action('wp_footer', 'asp_api_sample_code');
    function asp_api_sample_code() {
    	?>
    	<script>
    	jQuery(function ($) {
    		var settingsCheckboxToggle = function ($node) {
    			var $parent = $node;
    			var $checkbox = $node.find('input[type="checkbox"]');
    			var lvl = parseInt($node.data("lvl")) + 1;
    			var i = 0;
    			while (true) {
    				$parent = $parent.next();
    				if ($parent.length > 0 &&
    					typeof $parent.data("lvl") != "undefined" &&
    					parseInt($parent.data("lvl")) >= lvl
    				) {
    					if ($checkbox.prop("checked")) {
    						$parent.removeClass("hiddend");
    					} else {
    						$parent.addClass("hiddend");
    					}
    				} else
    					break;
    				i++;
    				if (i > 400) break; // safety first
    			}
    		}
    		$('.asp_option_cat input[type="checkbox"]').off('asp_chbx_change');
    		$('.asp_option_cat input[type="checkbox"]').on('asp_chbx_change', function (e) {
    			settingsCheckboxToggle($(this).closest('.asp_option_cat'));
    			var id = $(this).closest('.asp_w').data('id');
    			var instance = $(this).closest('.asp_w').data('instance');
    			ASP.api(id, instance, 'searchFor');
    		});
    	});
    	</script>
    	<?php
    }

    With this the checkbox states should stay the same as they are initially. If they are set to unchecked by default, then they should stay unchecked after clicking on the parent.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #31872
    theonlyit59
    theonlyit59
    Participant

    Hi,

    I have tried your code but nothing happens, By default, all the parent category checkboxes are unchecked. When I click one of the parent category checkboxes, all of the related child category checkboxes are shown but all of them are checked which I don’t want. After adding the above code this is the output I’m getting. Please see the attachment for reference.

    Thank you

    Regards,
    Shayan Ahmed

    Attachments:
    You must be logged in to view attached files.
    #31886
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    It is probably not possible then. If you want, you can leave a link where I could check this out, but blindly I probably can not suggest anything else.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #31890
    theonlyit59
    theonlyit59
    Participant
    You cannot access this content.
    #31898
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I think the code is executed too soon. Try this variation instead:

    add_action('wp_footer', 'asp_api_sample_code', 9999999999);
    function asp_api_sample_code() {
    	?>
    	<script>
    	jQuery(function ($) {
    		var settingsCheckboxToggle = function ($node) {
    			var $parent = $node;
    			var $checkbox = $node.find('input[type="checkbox"]');
    			var lvl = parseInt($node.data("lvl")) + 1;
    			var i = 0;
    			while (true) {
    				$parent = $parent.next();
    				if ($parent.length > 0 &&
    					typeof $parent.data("lvl") != "undefined" &&
    					parseInt($parent.data("lvl")) >= lvl
    				) {
    					if ($checkbox.prop("checked")) {
    						$parent.removeClass("hiddend");
    					} else {
    						$parent.addClass("hiddend");
    					}
    				} else
    					break;
    				i++;
    				if (i > 400) break; // safety first
    			}
    		}
    
    		setTimeout(function(){
    			$('.asp_option_cat input[type="checkbox"]').off('asp_chbx_change');
    			$('.asp_option_cat input[type="checkbox"]').on('asp_chbx_change', function (e) {
    				settingsCheckboxToggle($(this).closest('.asp_option_cat'));
    				var id = $(this).closest('.asp_w').data('id');
    				var instance = $(this).closest('.asp_w').data('instance');
    				ASP.api(id, instance, 'searchFor');
    			});
    		}, 500);
    	});
    	</script>
    	<?php
    }

    This should move the script to the bottom and should override the behavior.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #31900
    theonlyit59
    theonlyit59
    Participant
    You cannot access this content.
    #31907
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    If the items are actually assigned to the parent category as well, then try this changing option here: https://i.imgur.com/BHManGY.png
    If they are not assigned to the parent category, then unfortunately that is not possible.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #31925
    theonlyit59
    theonlyit59
    Participant
    You cannot access this content.
    #31927
    theonlyit59
    theonlyit59
    Participant
    You cannot access this content.
    #31953
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    With that code the child category checkboxes does not get unchecked for sure. I have double checked.
    For example after loading the page and and clicking “Server management”:
    – Initially the “Backup restore” is unchecked: https://i.imgur.com/aWvBse6.png
    – Then I click “Server management”, the “Backup restore” still unchecked: https://i.imgur.com/6g3O6wS.png
    – Then I click “Server management” again, the “Backup restore” still unchecked: https://i.imgur.com/mgBX2An.png

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #31955
    theonlyit59
    theonlyit59
    Participant

    Well brother,

    I also tested these steps.

    – Initially the “Backup restore” is unchecked.
    – Then I click “Server management”, the “Backup restore” still unchecked.
    – Then I click the “Backup restore” it changes to checked state.
    – Then I click “Server management” again while keeping the “Backup Restore” as checked, the “Backup restore” remains checked which I don’t want. What I want is when the parent category gets unchecked all the child categories should be unchecked as well. I Hope you got my point.

    Thank you

    Regards,
    Shayan Ahmed

    Attachments:
    You must be logged in to view attached files.
    #31960
    theonlyit59
    theonlyit59
    Participant

    Plus, when I check any child category while keeping the parent category as checked. Results are showing according to parent category which includes all the child categories. Child categories are not further getting filtered.

    Thank you

    Regards,
    Shayan Ahmed

    Attachments:
    You must be logged in to view attached files.
    #31978
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I’m afraid you may have to ask someone for customization for this specific case. The code I suggested is a good starting point, it prevents changing the checkbox statuses when the parent category is clicked – it only needs an addition to uncheck them when the parent is unchecked.
    Unfortunately this level of customization is way beyond our support duties, I hope you understand that.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #31982
    theonlyit59
    theonlyit59
    Participant

    Hmm…Thank you for all the support you have given.

    Regards,
    Shayan Ahmed

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.