DEV Community

Daniel Coturel
Daniel Coturel

Posted on

1

Get list of WooCommerce products and variations

Hi people. I just did something with JavaScript in a rush, and I would like to see how other people would solve it. Just to compare and because I'm not that good at JavaScipt, I think the answers would be enriching.

They gave me a WooCommerce website with products and variations loaded, and I needed to make a list to do other things on my ERP. The list should have:
1) Product ID
2) Variation ID
3) Variation SKU

I had to use 2 WooCommerce endpoints:

/wp-json/wc/v2/products

This lists all products. Every product has an id field

/wp-json/wc/v2/products//variations

This lists all variations of a given product id. Every variation has an id field and a sku field.

So, that's the challenge, get that list to save it as a CSV file. How would you do that?

Saludos,

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (1)

Collapse
 
theoutlander profile image
Nick Karnik

This is a general design problem.

If I were calling this from a frontend where I had an intermediate server, I would create a single endpoint on my server side which would call the other two URL's. Then, I would aggregate everything and return a response. In addition, I would make it somewhat flexible so I could pass it the type of format I want the data returned in. For instance {host}/api/products/variations?format=csv.

If this were in the backend, you can do it similarly with a function call that would make a call to the two endpoints and merge the data.

On the other hand, if I were calling WooCommerce directly from my frontend, I would create a function as mentioned earlier and fetch the data from both the endpoints and aggregate it. You can use something like Promise.all to accomplish that.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay