Wordpress REST API search products

Viewed 7701

Hope that you can help me.

I have a website that runs wooccommerce. I am using the woocommerce rest api. Now on another website what I want to do is create a simple search field where I type something and the search form needs to search through the woocommerce website and return results.

Is there a way I can achieve that ?

5 Answers

Try this:

/wp-json/wc/v2/products?search={{product_name}}

Works for me.

/wp-json/wc/v2/products?search={{product_name}}

Is this function working if you are looking a exact product?

If you have a product like MR2050 and another with MR2050K, the result is not as you may expected, cause API rest will return 2 products, not just ONE?

If you use a wc-api-php (PHP library), please follow like this :

require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'http://mydomain', // Your store URL
    'ck_****', // Your consumer key
    'cs_****', // Your consumer secret
    [
        'wp_api' => true, // Enable the WP REST API integration
        'version' => 'wc/v3' // WooCommerce WP REST API version
    ]
);

echo "<pre>";
print_r($woocommerce->get('products', ['search' => 'key word']));
echo "</pre>";

This works for me in 2021:

/wp-json/wc/v3/products?search={{productName}}

productName - refers to the input that you enter in the Search box

Related