kloon / WooCommerce-REST-API-Client-Library

A PHP wrapper for the WooCommerce REST API
GNU General Public License v3.0
356 stars 175 forks source link

Filtering product by category #200

Open S-Amin opened 8 years ago

S-Amin commented 8 years ago

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

matheusmaximo commented 7 years ago

Looking for the same...

apokyro commented 7 years ago

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.

rito-zainmax commented 7 years ago

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

apokyro commented 7 years ago

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() );
    }
}