How to use the Woocommerce API Without Knowing How to Code

WooCommerce
How to use the Woocommerce API Without Knowing How to Code

In this guide, I’ll show you how to get started using the Woocommerce API to add/update/delete orders, products, and customers in your store.

I’ve included plenty of examples of GET and PUT requests that you can copy or modify on your own for your own use.

Let’s begin!

Additional Resources

Step 1: Enable API access in Woocommerce

To enable the the Woocommerce API, login to the backend of your WordPress site, hover over Woocommerce > Settings > Advanced.

Next toggle the “Legacy API” tab > enable > Save Changes.

Step 2: Add a API keys with Read/Write access

Next, click on the “REST API” tab > add a key with your details.

woocommerce-api-key-details

For this tutorial, I recommend to give “Read/Write” access to test GET and PUT requests.

Once finished, click the Generate API key button to view your consumer_key and consumer_secret.

woocommerce-api-consumer-keys-secret

After generating your API keys, keep these keys in a safe place as we will be using them to connect to Insomnia, our API client.

Step 3: Download Insomnia or other API client

Now is the time to use our API keys to connect to our Woocommerce site with an API client.

Download Insomnia (not an affiliate link), the API client we will use to test GET and PUT requests.

Step 4: Use your API keys to connect to Insomnia

After downloading Insomnia, open the application and click “New Request”.

Under the “Basic” tab dropdown, click on “Basic Auth” and enter following credentials:

Next, enter your consumer_key and consumer_secret for the API keys you generated on your Woocommerce dashboard.

  • username = consumer_key
  • password = consumer_secret

GET request: view all orders

To start, I always do a GET request to get all orders just to make sure my API keys are working correctly.

In the GET request, enter a GET request to view all orders: https://yoursite.com/wp-json/wc/v3/orders

You should see all orders in your store.

PUT request: update a single order

To update an order, type in a GET request to view a single order:

https://yourcompany.com/wp-json/wc/v3/orders/{insert order ID}

For my example, the GET request was the following URL: https://b2bdemo.sgwebpartners.com/wp-json/wc/v3/orders/39645

Next, click the “GET” dropdown and choose “PUT” option

Next, go to the dropdown to choose “JSON”.

Use this example to update the order status from “processing” to “completed”

{
  "status": "completed"
}

Helpful tips

  • The consumer_key should start with “ck_”
  • The consumer_secret should start with “cs_”.
  • If necessary, you always can regenerate your API keys again.

Woocommerce GET request examples

If you are having retrieving any of these examples, make sure you scroll up to how to do GET requests.

GET request to view all orders

https://yourcompany.com/wp-json/wc/v3/orders/

GET request to view a single order

https://yourcompany.com/wp-json/wc/v3/orders/{insert order ID}

For example, the order ID is 82513, so I would use the following URL path to retrieve this order: https://yourcompany.com/wp-json/wc/v3/orders/82513 to retrieve a single order.

Tip: Find the order ID by going to clicking on the order and looking at number after “post=”. See the screenshot below for reference.

GET request to view all products

https://yourcompany.com/wp-json/wc/v3/products/

GET request to view a single product

https://yourcompany.com/wp-json/wc/v3/products/{insert product ID}

GET request to view a single product variation

https://yourcompany.com/wp-json/wc/v3/products/{insert product ID}/variations/{insert variation ID}

GET request to view all customers

https://yourcompany.com/wp-json/wc/v3/customers/

GET request to view a single customer

https://yourcompany.com/wp-json/wc/v3/customers/{insert customer ID}

Woocommerce PUT request examples

Pro tip: If you are having retrieving any of these examples, make sure you scroll up to how to do PUT requests.

PUT request to update price of a simple product

To update the “price” or “regular_price”, here is a example PUT request:

https://yourcompany.com/wp-json/wc/v3/products/{insert product ID}

Click “Body” > “JSON” and enter this query:

{
"regular_price": "81"
}

PUT request to update price and stock quantity

To update the “price” or “regular_price” and the “stock_quantity”, here is a example PUT request:

{
 "regular_price": "81”,
 “stock_quantity": 45
}

PUT request to update product meta_data

To update product meta data, here is a example PUT request

{
  "meta_data": [
    {
      "key": "_net_price",
      "value": "40"
    }
  ]
}

Pass the “key” as name of database field, and value as value.

PUT request to update price of product variant

example url: https://yourdomain.com/wp-json/wc/v3/products/833/variations/854

To update the “price” or “regular_price”, here is a example PUT request:

Click “Body” > “JSON” and enter this query

{
 "regular_price": "81"
}

Pass the “key” as name of database field, and “value” as value.

PUT request to update customer’s name

example url: https://b2bdemo.sgwebpartners.com/wp-json/wc/v3/customers/3144/

  • Replace https://b2bdemo.sgwebpartners.com with your domain
  • Replace 3144 with a specific customer ID in your store.
  • Find customer ID by clicking “edit” on the specific customer in Woocommerce and looking for the value after user_id=XXXX. In the example below, the customer ID is 3144

woocommerce-api-customer-id

To update the “first_name” and “last_name”, here is a example PUT request:

Click “Body” > “JSON” and enter this query

{
  "first_name": "John",
  "last_name": "Doe"
}

PUT request to update customer’s shipping address

{
  "shipping": {
    "first_name": "John",
    "last_name": "Doe",
    "company": "Demo Company",
    "address_1": "110 Demo Drive",
    "address_2": "Suite 110",
    "city": "Minneapolis",
    "state": "MN",
    "postcode": "94203",
    "country": "US"
  }
}

Helpful Tips and Additional Resources

If you have any questions or looking for more examples, let me know in the comments.

I will do my best to help in a timely manner and also add additional examples to this post for future readers.

Comments

  1. Wesley Townie April 21, 2019 | 8:08 pm

    Hey thanks Simon for all the help. Do you know how I would be able to update a customers shipping address with api?

    • Simon Gondeck April 21, 2019 | 9:47 pm

      Wesley – glad you enjoyed it! I added an example at the end of the tutorial on how to update a customer’s shipping address.

Leave a reply

Your email address will not be published. Required fields are marked*