skip to Main Content

i am developing mobile application to connect WooCommerce wordpress V3 rest API in iOS and Android.

i am getting the product list from Rest API

“wp-json/wc/v3/products”

i am able to filter product using

“wp-json/wc/v3/productscategory=241&attribute=pa_flavor&attribute_term=301”

and filter multiple attribute term using

“wp-json/wc/v3/products?attribute=pa_flavor&attribute_term=301,302,305”.

i need to filter product with multiple attribute(like pa_flavor,pa_size,pa_color… etc.) in rest API. how can do that?

2

Answers


  1. I think the question here isn’t how But Why do it that way? And which way is the best approach for you?

    What your Describing is basically categories so if that’s the case yes you can.

    https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product-category

    The Request:

    curl https://example.com/wp-json/wc/v3/products/categories/9

    /wp-json/wc/v3/products/categories/

    Now if you want to pull via Attributes

    You can Retrieve the Attributes them selves with

    /wp-json/wc/v3/products/attributes

    I’d ask that you read the Documents on the Woocoomerece Api It’s pretty extensive to be honest.

    If you want full control of this. I’d recommend setting categories or Tags and then adding products under said Categories or Tags then pull those tags/Categories

    Now you mention your making this for IOS and Android But what framework are you using.
    So i can provide you with some actual code examples of this.

    Login or Signup to reply.
  2. Create Custom REST API for Filter Products by multiple attributes

    // Create Custom REST API for Filter
    add_action('rest_api_init', 'wp_rest_filterproducts_endpoints');
    function wp_rest_filterproducts_endpoints($request) {
        register_rest_route('wp/v3', 'filter/products', array(
            'methods' => 'GET',
            'callback' => 'wp_rest_filterproducts_endpoint_handler',
        ));
    }
    
    function wp_rest_filterproducts_endpoint_handler($request = null) {
        $output = array();
        
        $params = $request->get_params();
    
        $category = $params['category'];
                
        $filters  = $params['filter'];
        $per_page = $params['per_page'];
        $offset   = $params['offset'];
        $order    = $params['order'];
        $orderby  = $params['orderby'];
        
        // Use default arguments.
        $args = [
          'post_type'      => 'product',
          'posts_per_page' => 10,
          'post_status'    => 'publish',
          'paged'          => 1,
        ];
        
        // Posts per page.
        if ( ! empty( $per_page ) ) {
          $args['posts_per_page'] = $per_page;
        }
    
        // Pagination, starts from 1.
        if ( ! empty( $offset ) ) {
          $args['paged'] = $offset;
        }
    
        // Order condition. ASC/DESC.
        if ( ! empty( $order ) ) {
          $args['order'] = $order;
        }
    
        // Orderby condition. Name/Price.
        if ( ! empty( $orderby ) ) {
          if ( $orderby === 'price' ) {
            $args['orderby'] = 'meta_value_num';
          } else {
            $args['orderby'] = $orderby;
          }
        }
        
            // If filter buy category or attributes.
        if ( ! empty( $category ) || ! empty( $filters ) ) {
          $args['tax_query']['relation'] = 'AND';
    
          // Category filter.
          if ( ! empty( $category ) ) {
            $args['tax_query'][] = [
              'taxonomy' => 'product_cat',
              'field'    => 'slug',
              'terms'    => [ $category ],
            ];
          }
    
          // Attributes filter.
          if ( ! empty( $filters ) ) {
            foreach ( $filters as $filter_key => $filter_value ) {
              if ( $filter_key === 'min_price' || $filter_key === 'max_price' ) {
                continue;
              }
    
              $args['tax_query'][] = [
                'taxonomy' => $filter_key,
                'field'    => 'term_id',
                'terms'    => explode( ',', $filter_value ),
              ];
            }
          }
    
          // Min / Max price filter.
          if ( isset( $filters['min_price'] ) || isset( $filters['max_price'] ) ) {
            $price_request = [];
    
            if ( isset( $filters['min_price'] ) ) {
              $price_request['min_price'] = $filters['min_price'];
            }
    
            if ( isset( $filters['max_price'] ) ) {
              $price_request['max_price'] = $filters['max_price'];
            }
    
            $args['meta_query'][] = wc_get_min_max_price_meta_query( $price_request );
            }
        }
        
        $the_query = new WP_Query( $args );
    
        if ( ! $the_query->have_posts() ) {
          return $output;
        }
                            
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $product = wc_get_product( get_the_ID() );  
    
            // Product Properties
            $wcproduct['id'] = $product->get_id();
            $wcproduct['name'] = $product->get_name();
                        
            $output[] = $wcproduct;
        }
        wp_reset_postdata();
    
        return new WP_REST_Response($output, 123);
    }
    

    The request:

    /wp-json/wp/v3/filter/product/?category=kurtis&filter[pa_color]=XX,X,XXX

    For Price:

    /wp-json/wp/v3/filter/product/?filter[min_price]=500&filter[max_price]=1000&filter[pa_color]=XX

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search