Select category

  • Author
    Posts
  • vheo

    Hello, how can i change category select field in search form to not to display only actual tree of category with current as a root? E.g. if we have categories like
    -Cat1
    -Cat2
    –Cat2.1
    –Cat2.2
    -Cat3
    And i selected cat2 then options in select field are only cat2, cat2.1 and cat2.2 but i’d like to have also choose with cat1 and cat3. Is it possible? I think that its sth about options array filtering in class-select but firstly i’d ask dev 😉

    vheo
    $options = array_filter(
    			$this->options,
    			function( $option ) use ( $current ) {
    				$parent = hp\get_array_value( $option, 'parent' );
    
    				return ( is_null( $current ) && is_null( $parent ) ) || ( ! is_null( $current ) && $parent === $current );
    			}
    		);

    If i try to disable filter or delete scnd condition (! is_null( $current ) && $parent === $current) there is Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes)

    ihordeveloper

    There’s no easy way to do this unfortunately (if you mean expanding the whole category tree by default), if you’re familiar with PHP please try to use the “hivepress/v1/forms/listing_filter” hook:

    add_filter(
    	'hivepress/v1/forms/listing_filter',
    	function( $form ) {
    		$form['fields']['_category']['options'] = [
    			1 => 'custom',
    			2 => 'options',
    			3 => 'here',
    		];
    
    		return $form;
    	},
    	999
    );

    Please never edit the HivePress file directly because any changes will be erased on update. Also, here’s a link to related topic that may help https://hivepress.io/support/topic/hello/#post-3108

    vheo

    Now it’s hard to explore category, e.g. when i’m in cat 2 i can not go to cat 1 without getting back to ‘all categories’ :((
    So now the only way is to insert options by static code? I just want to disable filter which return false for categories which are not in current parent category.

    vheo

    Here’s example. http://zamowstronke.pl/kwatery/oferty/
    Try this:
    1. Select category e.g. “1 os. pokoje na stancji”
    2. Try to change category to another e.g. “1 os. pokoje w mieszkaniu studenckim”

    You can not do it without going to “Wszystkie categories (All categories)” between 1 and 2nd step 🙁

    ihordeveloper

    This was the initial idea, because showing all categories may confuse users (if there are a lot of categories), the assumption is that users will narrow down their search by selecting categories from parent to child and they don’t need to return one level back (at least often). It works like with folders in the filesystem, you see only contents of the current folder and the folder path.

    You can try adding a separate attribute for the number of rooms, and use categories for apartment types instead, so users will be able to switch the number of rooms without changing the category.

    Also, I plan to add an option for moving the category filter to the search bar so there would be a drop-down with all categories, but there’s no ETA for this feature.

    ihordeveloper

    If you’re familiar with PHP you can supply the category tree as a list of options in the code snippet suggested above, e.g. with this function https://developer.wordpress.org/reference/functions/get_terms/

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

You must be logged in to reply to this topic.

Subscribe

Sign In

Don't have an account? Register

Forgot password?

Register

Already have an account? Sign In

Forgot password?

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.