• Resolved guardiano78

    (@guardiano78)


    Hello,

    this is the first time I trying to use Woocommerce REST API.
    I’m reading the documentation.

    By using the following code, I’m able to get all customers:

    <?php
    
    $api_url_users = 'https://mywebsite.com/wp-json/wc/v3/customers';
    $api_key = "xxxxxx";
    $api_secret = "xxxxxx";
    
    // params
    $params = array(
        'per_page' => 100,
        'role' => 'all',
    );
    
    // header
    $headers = array(
        'Authorization: Basic ' . base64_encode($api_key . ':' . $api_secret)
    );
    
    $url = $api_url_customers . '?' . http_build_query($params);
    
    
    // cURL options
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_SSL_VERIFYPEER => true, 
    ));
    
    // response
    $response = curl_exec($curl);
    curl_close($curl);
    
    
    // JSON decode
    $customers = json_decode($response, true);

    It works! … but now I need to get all customers that has a specific meta_key empty. For example, if I need to get only customers that has postcode empty, in your opinion, is it possible?
    I tried with something like that, but it doesn’t work:

    $params = array(
        'per_page' => 100,
        'role' => 'all',
        'meta_query' => array(
            array(
                'key' => 'postcode',
                'value' => '',
            ),
        ),
    );

    Maybe “search” param it can help me, but I can’t found any example for how to use search in REST API. I don’t know, maybe i had some confusion. Anyone can help me?

    Thank you all.

    • This topic was modified 1 year, 4 months ago by guardiano78.
Viewing 1 replies (of 1 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @guardiano78

    Unfortunately, the WooCommerce REST API does not directly support querying customers by meta fields, like ‘postcode’. The ‘meta_query’ parameter you’ve used is not officially supported in WooCommerce REST API.

    However, you can achieve this by creating a custom endpoint in your WordPress plugin or theme’s functions.php file. This custom endpoint will use WP_User_Query, which does support meta_query, to fetch the users.

    Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any further questions on development or custom coding, don’t hesitate to reach out to some of our great resources available for support. Our WooCommerce community is brimming with skilled open-source developers who are active on the following channels:

    As for the ‘search’ parameter, it only searches for the given string in the customer’s id, email, and username. It does not search in the user meta fields.

    I hope this helps! Let us know if you have any other questions.

    Thanks!

Viewing 1 replies (of 1 total)
  • The topic ‘Get customers using REST API’ is closed to new replies.