WooCommerce API Python. Brief guide for beginners

If you have an online store that is developed in WordPress with WooCommerce, it is likely that at some point you have had to export data for a report or to know the details of your orders. In this article, we are going to see some basic notions of the WooCommerce API Python for beginners and you will be able to download two templates to extract your data.

1. The WooCommerce API

The WooCommerce API for Python has a very extensive and well-structured documentation detailing how it works. Through it you can:

  • Retrieve order, product and customer information
  • Create and delete orders, products and customers
  • Process payments
  • Manage shipments

In this case, we are going to focus only on the first application: retrieving information about our orders.

The most commonly used objects in the WooCommerce API (some already mentioned) are:

  • Orders
  • Products
  • Customers
  • Product categories
  • Product tags
  • Product attributes
  • Coupons

And a few more elements. For this example, we will only use orders and products. Let’s move on now to find the credentials to authenticate to the API

2. Activating the API credentials

To create a valid key for the WooCommerce API, we will have to go, inside our WordPress to WooCommerce -> Settings -> Advanced -> API Rest:

Here we will click on “Add key”. Once here, we will give it a description that allows us to identify it and we will give it read permissions. For this use and for security reasons, “read” is enough. Write and read/write access can also be given. This would allow this key to modify your order information, which we do not intend to do.

After clicking on “Generate API key”, you will get a client key, whose characters start with “ck_” and a secret client key, whose characters start with “cs_”:

Copy these keys somewhere safe, because you will need them to be able to work with the scripts we are going to see next.

3. Download and use Python scripts

With the WooCommerce Python API we are going to work with two scripts:

  1. A script to retrieve order information and order items, which you can see here.
  2. A script to retrieve information about the products you have in your store, which you can see here.

Keep a copy of them, and you will be able to edit it. In both files, you will only need to modify the credentials information from step 2: Your website URL, client password and client secret password:

Once modified, just go to “Execution environment” -> “Run all” and the code will do:

  1. In the case of the order information, it will generate two excel files
    • One with the order data, with these fields:
      • order id
      • total
      • shipping_total
      • payment_method
      • date_paid
      • hour_paid
      • billing_postcode
      • billing_country
      • billing_email
      • cart_tax
      • shipping_tax
    • Another one with the data of the products purchased in each order, with these fields:
      • order id
      • date_paid
      • item_name
      • price
      • total
      • quantity
      • coupon_code
  1. In the case of product information, it will generate a file with the following information
    • Product data:
      • product id
      • name
      • permalink
      • type
      • status
      • catalog_visibility
      • stock_status
      • description
      • short_description
      • sku
      • price
      • regular_price
      • sale_price
      • on_sale
      • purchasable
      • virtual
      • downloadable
      • tax_status
      • tax_class
      • manage_stock
      • stock_quantity
      • weight
      • dimensions_length
      • dimensions_width
      • dimensions_height
      • shipping_required
      • shipping_taxable
      • shipping_class
      • tags_id
      • tags_name
      • categories_id
      • categories_name

You can add or remove more variables. Check the documentation and adapt it to the information you want to get:

A detail about the WooCommerce API Python

One last consideration. Through this file you will be able to retrieve up to 100 orders and 100 products. If you want to get more, just modify the “page” parameter, which by default is set to “1”, and you will get the 100 most recent orders. If you want to get from order 101 to 200, set the value “2”; from 201 to 300 set the value “3”, and so on.

With this, you would have everything to retrieve your order and product information in WooCommerce with Python through these templates, which I hope you will find useful.

Another option would be to use funnel.io to download the data, but perhaps we can look at that another time.

See you next time! :)

Leave a Comment