Change Number of Search Results for Specific User in Woocommerce

Normally Woocommerce pulls the number of results from the standard WordPress settings, usually set to 20-30 for most installs. For many environments it is helpful to keep this number relatively low for performance reasons. However, for specific logged in users, it can be helpful to set a higher default.

My client wanted his default set to 100. The following code snippet can be modified to fit your needs.

<?php
/**
 * Change number of products that are displayed per page (shop page)
 */
add_filter( 'loop_shop_per_page', 'custom_user_loop_shop_per_page', 20 );
function custom_user_loop_shop_per_page( $num_products) {
        $username = "test_user_name";
  // $num_products contains the current number of products per page based on the value stored on Options -> Reading
	$user = wp_get_current_user();
    if($user && isset($user->user_login) && $username == $user->user_login) {
        $num_products = 100;
    }
  return $parts;
}

Leave a comment

Your email address will not be published. Required fields are marked *