Create and Update Data in WooCommerce from Dataverse

Sophia Petrillo in awe at Power Automate integrating Dataverse and WooCommerce
Picture it: Dataverse and WooCommerce integrated with no plugins or costs.

This was another huge learning experience for me, but I can’t take all the credit for it. A post on LinkedIn from Andrew Hess making an API call to ChatGPT is what got me on the path to this. Now I am unstoppable with APIs from Power Automate.

Thank you Andrew for creating a monster!

Once I understood how to use the HTTP action in Power Automate, EVERYTHING MADE SENSE. All of the APIs are slightly different, but it just takes some time to study the vendor’s API documentation. Then you too can be a monster.

ON TO WOOCOMMERCE.

Since WordPress is used by like 40% of the Internet, WooCommerce is just another natural extension of using WordPress. You can customize it for basically ANY kind of shopping experience: products, downloads, memberships, sponsorships, auctions and on and on and on and on and on and on and on.

So, if your master system is Dynamics 365 or a Power Apps app, how can you keep WooCommerce products in sync? WITH POWER AUTOMATE!

LET’S START THE FUN!

Step 1: Create a WordPress User

First, we need to set up a specific user in WordPress that isn’t tied to any user account. You don’t want to have that account be disabled or deleted and then your API keys stop working. Go to the WordPress Admin > Users and create a new user. It needs to have a role of Administrator.

MAKE A SECURE PASSWORD.

Step 2: Generate the WooCommerce API Keys

Next, we need some API keys from WooCommerce. Go to your WooCommerce store settings in the WordPress Admin > WooCommerce > Settings > Advanced > REST API.

WooCommerce REST API settings

Click Add key and just follow the prompts. Make sure to select the user you just created and select the permissions as Read/Write.

WooCommerce REST API create new key

Generate your API key!

Your keys will show on the screen. COPY THEM TO A SAFE PLACE.

Step 3: Modifications to Your Power Apps App

We have to do a couple of things to our app first so data sync will be successful.

– Add a field to an entity (say, Products) that has a field called “WooCommerce ID” (you can call it whatever you want, I just wanted to be original) to store the ID when the product is created in WooCommerce.
– Add a yes/no field to allow the user to NOT sync items to WooCommerce. This field also doubles as the trigger to push an existing item over to WooCommerce.
– A business rule to show the WooCommerce ID field if “Yes” or hide the WooCommerce ID field if “No.”

You all know how to do that already. Moving on!

Step 4: Build Some Flows with the HTTP Connector

I am triggering this to run on the creation of a product in my app. So, a standard start to our Flow with one small change in the settings:

Flow trigger on Product

Click the dots and go to Settings:

I only want this to run if the Show On Website? field is selected “Yes.” We don’t need to run this flow pointlessly!

Flow trigger conditions

Next, time to build the rest of the flow….but….whaaaaaaaa? What is this?

Power Automate Try Catch Finally error handling

What the hell is Try, Catch, Finally?

Those are scopes in Power Automate and is how we do error handling! First we try, then if something goes wrong, we catch it, then finally the flow completes with success or the errors are emailed to someone. I’ll post about this in a future post!

Here’s the magic: the HTTP connector.

Power Automate HTTP connector

This is what we are going to use to perform a POST request to the WooCommerce API.

The HTTP connector configured in Power Automate to add a product to WooCommerce

Spelling it out more:

– The Method field is “POST” since we are sending data to WooCommerce.
– The URI field is the URL of your website and the API link to the products endpoint.
– There’s just one header: Content-Type: application/json
– The Body follows WooCommerce’s API instructions exactly of the fields you want to fill in or are required.
– Authentication is “Basic” and the username and password fields are the two API keys generated in Step 2.

A note about the API keys: you would want to store these in something like Azure Key Vault — and then have Power Automate make a connection to that. For this example, I have these stored in two fields in Dataverse, but that is not OK for production.

Another note about the API keys: you could probably store these in the database, but then use permissions and field-level security to hide it to everyone but an admin. That is me thinking out loud. You’d also have to turn on Secure Inputs and Secure Outputs on every flow that needed to use the keys.

ONWARDS!

Once we send our request to the WooCommerce API, a response will be received back in JSON. We have to parse that, so here comes the Parse JSON action.

Parse JSON action in Power Automate to do stuff with the response from the WooCommerce API

So, what goes in here?

In the Content field, it is the Body field from the HTTP action.

In the Schema field, this took me a minute to figure out. This is the template (for lack of a better word) of what the response from the API is going to be. So, you know, it knows what gets returned and then you can use that data in subsequent actions.

Now, time to test if calling the API was successful. A simple condition that checks if the status code returned is 201 (yes, 201 — WooCommerce uses 201 as successful and not 200).

Test for WooCommerce API status code in Power Automate.

If the API call fails for whatever reason (because these things happen), this will go into the Catch scope of the flow.

If successful, then Power Automate will update the item in Dataverse with the automatically-generated ID from WooCommerce.

Update Dataverse with WooCommerce ID.

This is why we created the WooCommerce ID field — so if this product ever changes in the future or need to be hidden from WooCommerce — we have to pass the ID to the API. Dataverse is always your main source of truth, right? More on that in a future blog.

WE ARE ALMOST THERE.

I create a product in Dataverse, like so.

Creating a product in Dataverse.

AND PRESTO CHANGE-O, there’s our product in WooCommerce!

The product from Dataverse in WooCommerce.

Whaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat?! Yep! There it is!

Another BIG BIG BIG thank you to Andrew Hess for his video that started this all.

Leave a Comment

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