Wordpress Rest API

  • 5 years, 5 months ago #11995

    Can we leverage this plugin’s search power using wordpress Rest API ?

    wpsolr
    Keymaster
    5 years, 5 months ago #11996

    No. We did not define our own public API that could be called from WP REST API.

    5 years, 5 months ago #11997

    If we perform a search from wordpress rest API does this plugin return the search from elasticsearch index ?

    Like it replaced the default WP search

    wpsolr
    Keymaster
    5 years, 5 months ago #11998

    Can you give me a code sample to test?

    5 years, 5 months ago #11999

    I use this code for my search API we hit it using our mobile application

    
         /**
         * Medicine Search
         * Get search results from WooCommerce
         * @group Open Web Services
         * @bodyParam q string Keywork for searching medicine. Example: para
         * @bodyParam paged int Page number for pagination. Example: 1
         */
        public function medicine_search(Request $request) {
            global $wpdb;
    
            $error_response = [
                'q'      => $request->input('q') ?? "",
                'paged'  => $request->input('paged') ?? 1
            ];
    
            try {
                $validator = Validator::make($request->all(), [
                    'q'      => 'required'
                ],[
                    'required' => 'The :attribute field is required.'
                ]);
    
                if ($validator->fails()) {
                    return $this->api_error_syntax($validator->errors()->first(), $error_response);
                }
    
                $search_term     = $request->input('q') ?? "";
                $paged           = $request->input('paged') ?? 1;
    
                $args = array(
                    'post_type' 		=> 'product',
                    'post_status'       => 'publish',
                    'stock' 			=> 1,
                    'orderby' 		 	=> 'date',
                    'posts_per_page' 	=> 10,
                    'paged'             => $paged,
                    'order' 			=> 'DESC',
                    's' 				=> $search_term,
                    'tax_query'             => array(
                        array(
                            'taxonomy'      => 'product_visibility',
                            'field'         => 'slug',
                            'terms'         => 'exclude-from-catalog',
                            'operator'      => 'NOT IN'
                        )
                    )
                );
                $loop = new \WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product;
    
                    $product_id = $product->id;
                    $terms      = wp_get_post_terms( $product_id, 'Manufacturer' );
                    $on_brand 	= '';
                    if ( $terms && ! is_wp_error( $terms ) ) {
                        $brand_links = array();
                        foreach ( $terms as $term ) {
                            $brand_links[] = $term->name;
                        }
                        $on_brand = join( ", ", $brand_links );
                    }
    
                    $on_category    = '';
                    $categories     = wp_get_post_terms( $product_id, 'product_cat' );
                    if ( $categories && ! is_wp_error( $categories ) ) {
                        $categories_links = array();
                        foreach ( $categories as $category ) {
                            $categories_links[] = $category->name;
                        }
                        $on_category = join( ", ", $categories_links );
                    }
                    $category_output = $on_category;
    
                    $product_arr['id'] 				= $product->id;
                    $product_arr['name'] 			= $product->post->post_title;
                    $product_arr['category'] 		= $category_output;
                    $product_arr['brand'] 		    = $on_brand;
                    $image 							= wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
                    $product_arr['image'] 			= (isset($image[0])) ? $image[0] : "";
                    $price_html 					= $this->price_array($product->get_price_html());
                    $price_split					= strip_tags($price_html);
                    $price_split					= str_replace(' '," ",$price_split);
                    $price_split					= str_replace('₨'," ",$price_split);
                    $product_arr['price'] 			= trim($price_split);
                    $product_arr['currency'] 		= get_option('woocommerce_currency');
                    $product_arr['content_html']	= $product->post->post_content;
                    $product_arr['content']			= strip_tags($product->post->post_content);
                    $product_arr['slug']			= $product->post->post_name;
                    $products['products'][] 		= $product_arr;
    
                endwhile;
                wp_reset_query();
    
                $products['pagination'] = array(
                    "total_pages"   => $loop->max_num_pages,
                    "current_page"  => $paged,
                    "next_page"     => (($paged + 1) <= $loop->max_num_pages) ? ($paged + 1) : "",
                    "previous_page" => (($paged - 1) > 0) ? ($paged - 1) : ""
                );
    
                return $this->api_response_syntax("Done",$products);
            }
            catch (\Exception $ex) {
                $this->log_error($ex,"medicine_search");
                return $this->api_error_syntax(Lang::get("responses.error_in_connection"));
            }
        }
    
    wpsolr
    Keymaster
    5 years, 5 months ago #12005

    WPSOLR replaces the main $wp_query object.

    It will not work on custom queries like
    $loop = new \WP_Query( $args );

    5 years, 5 months ago #12105

    Thanks for the reply, what if we use the default woocommerce rest api ?

    https://mysite.com/wp-json/wc/v2/products?search={{product_name}}

    Can you please verify ?

    5 years, 5 months ago #12186

    Can you please confirm this ?

    5 years, 5 months ago #12210

    Waiting for your response.

    wpsolr
    Keymaster
    5 years, 4 months ago #12222

    It will not work.

    5 years, 3 months ago #12783

    Is there any possibility to use this plugin with my mobile application ?

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

You must be logged in to reply to this topic.