Fetching by Categories in Woocommerce REST API

Categories Codes, Tutorials

You may need to retrieve a list of products in a certain category using the REST API, but unfortunately you can’t do that in the currents versions of Woocommerce API.

But here’s a tweak that will give you the result you need, you just need to add a little code inside the class-wc-api-products.php file.

Open up your favorite text editor or simply use WordPress code editor (Plugins\Editor, make sure you select Woocommerce in the ‘select plugin to edit’ list), and open wp-content/plugins/woocommerce/includes/api/class-wc-api-products.php

Now find the query_products($args) function and add the following line to the $query_args  array:

'product_cat' => $args['product_cat']

 

So, the query_products function shoud look like this:

private function query_products( $args ) {
  // Set base query arguments
  
  $query_args = array(
    'fields'      => 'ids',
    'post_type'   => 'product',
    'product_cat' => $args['product_cat'],
    'post_status' => 'publish',
    'meta_query'  => array(),
  );
  // Taxonomy query to filter products by type, category, tag, shipping class, and
  // attribute.
  $tax_query = array();

  // Map between taxonomy name and arg's key.
  $taxonomies_arg_map = array(
    'product_type'           => 'type',
    'product_cat'            => 'category',
    'product_tag'            => 'tag',
    'product_shipping_class' => 'shipping_class',
  );

  // Add attribute taxonomy names into the map.
  foreach ( wc_get_attribute_taxonomy_names() as $attribute_name ) {
    $taxonomies_arg_map[ $attribute_name ] = $attribute_name;
  }

  // Set tax_query for each passed arg.
  foreach ( $taxonomies_arg_map as $tax_name => $arg ) {
    if ( ! empty( $args[ $arg ] ) ) {
      $terms = explode( ',', $args[ $arg ] );

      $tax_query[] = array(
        'taxonomy' => $tax_name,
        'field'    => 'slug',
        'terms'    => $terms,
      );

      unset( $args[ $arg ] );
    }
  }

  if ( ! empty( $tax_query ) ) {
    $query_args['tax_query'] = $tax_query;
  }

  // Filter by specific sku
  if ( ! empty( $args['sku'] ) ) {
    if ( ! is_array( $query_args['meta_query'] ) ) {
      $query_args['meta_query'] = array();
    }

    $query_args['meta_query'][] = array(
      'key'     => '_sku',
      'value'   => $args['sku'],
      'compare' => '='
    );

    $query_args['post_type'] = array( 'product', 'product_variation' );
  }

  $query_args = $this->merge_query_args( $query_args, $args );

  return new WP_Query( $query_args );
}

As you can see, we added our code in the line 7,  so now we should be able to filter products by categories in the URL by adding filter[‘product_cat’] = ‘category-slug’

Example:

http://www.mywoocommerce.com/wc-api/v1/products?filter[‘product_cat’]=’shoes’&etc

Now, Woocommerce should only returns the products which fall under the ‘Shoes’ category

Leave a Reply

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