Support » Plugin: Advanced AJAX Product Filters » Display subcategories

  • Resolved fkoomek

    (@fkoomek)


    Hi.
    Trying to make your filter work while both products and subcategories/categories are displayed. So in order to give subcategories different CSS class I used this code:

    /*Move WooCommerce subcategory list items into their own <ul> separate from the product <ul>.*/
    add_action( 'init', 'move_subcat_lis' );
    function move_subcat_lis() {
    	// Remove the subcat <li>s from the old location.
    	remove_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );
    	add_action( 'woocommerce_before_shop_loop', 'msc_product_loop_start', 1 );
    	add_action( 'woocommerce_before_shop_loop', 'msc_maybe_show_product_subcategories', 2 );
    	add_action( 'woocommerce_before_shop_loop', 'msc_product_loop_end', 3 );
    }
     //Conditonally start the product loop with a <ul> contaner if subcats exist.
     
    function msc_product_loop_start() {
    	$subcategories = woocommerce_maybe_show_product_subcategories();
    	if ( $subcategories ) {
    		echo '<ul class="product-cats">';
    
    	}
    }
    //Print the subcat <li>s in our new location.
    function msc_maybe_show_product_subcategories() {
    	echo woocommerce_maybe_show_product_subcategories();
    }
    //Conditonally end the product loop with a </ul> if subcats exist.
    function msc_product_loop_end() {
    	$subcategories = woocommerce_maybe_show_product_subcategories();
    	if ( $subcategories ) {
    		echo '<div style="clear:both;"></div></ul>';
    	}
    }

    It’s working good. The categories have separated class product-cats, and so there is no conflict in displaying filtered products in the right div. Strange thing is that when I filter products and then refresh the page, subcategories disappears.
    Check: https://www.test2.liberlapidum.com/test2/

    Steps to reproduce:
    1. Filter products on the main page by using filter (at the bottom).
    2. Refresh the page or just click on one of the products and then go back.
    3. Sub categories disappear.

    Thanks

    • This topic was modified 5 months ago by  fkoomek.
    • This topic was modified 5 months ago by  fkoomek.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Dima Holovnia

    (@dholovnia)

    Hi,

    Product categories and products are same WooCommerce main query. The reason is that after ajax filter 1 is replaced with update html and another is not. But both are influenced.

    Right now we don’t have an option to stop applying filters to some elements on the page.

    Btw how are you outputting categories? Shortcode or some widget?

    Regards,
    Dima

    fkoomek

    (@fkoomek)

    I am outputting categories by choosing to display them in product category settings (I have set “both”).
    By default, categories are displayed mixed with products so I am using the PHP code above to remove them from product loop and move into their own place woocommerce_before_shop_loop. I was able to add them their own CSS class product-cats. By default products and categories have exactly the same class.

    fkoomek

    (@fkoomek)

    fkoomek

    (@fkoomek)

    Is there any quick fix for stopping affecting categories loop by your filter?

    Plugin Author Dima Holovnia

    (@dholovnia)

    Hi,

    Please open plugin settings page tab Advanced and check if you have option ‘Display products’ turned on. If yes, turn it off, save the settings and check your page again.

    Regards,
    Dima

    fkoomek

    (@fkoomek)

    Great, thanks!
    This is what I was looking for.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.