Woocommerce Exclude out of stock products from the product loop

Exclude out of stock products from the woocommerce product loop

The WooCommerce plugin is a powerful tool for creating an online store on a WordPress website. It provides a range of features and functions for managing products, orders, and customers. One important aspect of any online store is the way that products are displayed and searched for by customers. The product loop query is the code responsible for fetching and displaying products on the website. By optimizing the product loop query, you can improve the performance and user experience of your online store.

In this article, we will look at a sample function that you can use to optimize the product loop query in WooCommerce. The function hooks into the pre_get_posts action and modifies the main query on the product archive page. It sets the number of products per page to 12, sorts the products by price in ascending order, and excludes out of stock products from the loop.

Here is the code for the function:

function custom_product_loop_query( $query ) {
  if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'product' ) ) {
    // Set the number of products per page
    $query->set( 'posts_per_page', 12 );

    // Sort products by price in ascending order
    $query->set( 'orderby', 'price' );
    $query->set( 'order', 'asc' );

    // Exclude out of stock products from the loop
    $query->set( 'meta_query', array(
      array(
        'key'     => '_stock_status',
        'value'   => 'outofstock',
        'compare' => 'NOT IN'
      )
    ) );
  }
}
add_action( 'pre_get_posts', 'custom_product_loop_query' );

Let’s take a closer look at what each part of the function does.

The first line of the function checks if the current query is not in the admin area, is the main query on the page, and is for the product post type archive. This is important because we only want to modify the main query for the product archive page, and not any other queries on the site.

The next three lines of the function use the set method to modify the query arguments. The posts_per_page argument determines the number of products that will be displayed per page. In this case, we have set it to 12. The orderby and order arguments determine the sort order of the products. In this case, we are sorting the products by price in ascending order.

Finally, the meta_query argument is used to exclude out of stock products from the loop. The key parameter specifies the metadata field to query, and the value parameter specifies the value that we want to exclude. In this case, we are looking for products with a stock status of “outofstock” and excluding them from the loop.

Once the function has been defined, we use the add_action function to hook it into the pre_get_posts action. This ensures that the function is executed before the main query is run on the product archive page.

Using this function, you can easily customize the product loop query to suit your specific needs. For example, you can change the number of products per page, the orderby and order parameters, or the meta query

Similar Posts