WooCommerce-REST-API-Client-Library icon indicating copy to clipboard operation
WooCommerce-REST-API-Client-Library copied to clipboard

Filtering product by category

Open S-Amin opened this issue 9 years ago • 4 comments

Hey there has been there post in first page of issues that hasn't been solved. Is there any body to solve this??? It's really important. at least show us how to use WP_Query filter in your library

S-Amin avatar Sep 11 '16 14:09 S-Amin

Looking for the same...

matheusmaximo avatar Oct 04 '16 16:10 matheusmaximo

Hi @S-Amin ,

I have managed to filter products by category like this:

$client->products->get( null, array( 'filter[category]' => category_slug_here, 'filter[limit]' => '-1' );

Use limit -1 in order to get all products.

apokyro avatar Dec 04 '16 11:12 apokyro

Hi @apokyro for me doesn't work. Could you upload your full code?

rito-zainmax avatar Dec 05 '16 11:12 rito-zainmax

Hi @rito-zainmax , check below.


$options = array(
    "debug"           => false,
    "return_as_array" => true,
    "validate_url"    => false,
    "timeout"         => 30,
    "ssl_verify"      => false,
);
try {

	$client = new WC_API_Client(
		"http://your-store-url.com",
		"ck_enter_your_consumer_key",
		"cs_enter_your_consumer_secret",
		$options
	);

	print_r($client->products->get( null, array(
                "filter[category]" => "CATEGORY_SLUG_HERE",
                "filter[limit]"    => "-1" // -1 to display all products
            )
        ));

} catch ( WC_API_Client_Exception $e ) {

	echo $e->getMessage() . PHP_EOL;
	echo $e->getCode() . PHP_EOL;

	if ( $e instanceof WC_API_Client_HTTP_Exception ) {

		print_r( $e->get_request() );
		print_r( $e->get_response() );
	}
}

apokyro avatar Dec 07 '16 19:12 apokyro