Forums Forums Search & Filter Pro Make “Posts Per Page” work with Woocommerce theme support for product_grid

Viewing 2 posts - 1 through 2 (of 2 total)
  • Anonymous
    #247946

    Howdy,

    This was originally going to be a support question, but I ended up finding a way to make it work for me. if anything, I’d love feedback on whether or not you had a better method. Otherwise, I’m dropping my code snippet here for other users to find.

    Woocommerce has a custom theme support for theme developers that allows for specifying the number of rows and columns on a catalog page. https://docs.woocommerce.com/document/woocommerce-theme-developer-handbook/#section-7

    In Search & Filter Pro, there is a setting to specify the results per page, which will completely override this customizer setting.

    Here is the code snippet I used to get around this:

    function prefix_filters_post_count( $query_args, $sfid ) {
        // If search form ID = 150, the do something with this query. edit to your form ID.
    	if ( 150 === $sfid ) {
            if ( function_exists( 'wc_get_theme_support' ) ) {
                $product_grid = wc_get_theme_support( 'product_grid' );
    
                $rows = isset( $product_grid['default_rows'] ) ? get_option( 'woocommerce_catalog_rows', absint( $product_grid['default_rows'] ) ) : 5; // 5 rows as a fallback
                $columns = isset( $product_grid['default_columns'] ) ? get_option( 'woocommerce_catalog_columns', absint( $product_grid['default_columns'] ) ) : 3; // 3 cols as a fallback
    
                $count = max( 1, $rows * $columns ); // return rows*columns, or fallback to 1 in case of error
    
                $query_args['posts_per_page'] = $count;
            }
    	}
    
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'prefix_filters_post_count', 20, 2 );
    Trevor
    #247967

    Wow. I am sure other users will find that most helpful. Thanks for sharing. I will close this thread for now.

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