Woocommerce workflow

Hello there,

I want to fetch products from an api and post the products to woocommece using the woocommerce api…

The productinfo comes in json and i think i need to transform the json a bit… i.e. i want to have stock quintity in woocommerce to exist from a sum of 2 fields of the json…

There are about 5000 products i want to add…

If i change the output of the json with javascript in a function and want to post it in batches to woocommerce products… how can i do it so it creates seperate products for every product from the api? I tried to connect the api with woocommerce directly and it puts all information under 1 product instead of making separate products…

Is it possible to build a workflow with n8n for this? Whats the best way to go?

I tried to do this:

I use this to transform data:

// Haal de originele JSON-gegevens op van de inkomende input
const originalData = items[0].json;

// Functie om productgegevens te transformeren
function transformProduct(product) {
  return {
    name: product.title,
    slug: product.title.toLowerCase().replace(/\s+/g, "-"),
    date_created: "2023-08-16T00:00:00", // Pas de datum aan
    type: "simple",
    status: "publish",
    description: "",
    short_description: "",
    regular_price: product.price.toFixed(2),
    categories: [
      {
        id: product.categoryid1,
      },
      {
        id: product.categoryid2,
      },
    ],
    images: [
      {
        src: product.productimage,
      },
    ],
    // ... Voeg hier andere velden toe die je wilt overnemen
  };
}

// Transformatie van elk product
const transformedProducts = originalData.map(transformProduct);

// Stuur de getransformeerde producten als uitgaande gegevens
return [{ json: transformedProducts }];

I get this error:

Problem in node ‘Code1‘

Cannot read properties of undefined (reading ‘map’) [line 37]

This is the structure of my original JSON:

[
{
“itemcode” : “SP30SB157000001”,
“title” : “Creative Sound Blaster”,
“longtitle” : “Creative Sound Blaster”,
“description” : “Creative Sound Blaster”,
“brandcode” : “B3”,
“brand” : “Creative Labs”,
“groupcode” : “K7”,
“group” : “SOUND”,
“statusgroupcode” : “10009”,
“statusgroup” : “New”,
“price” : 21.422499999999996,
“chargeprice” : 0,
“totalprice” : 21.422499999999996,
“suggestprice” : 35.5,
“barcode” : “”,
“status” : “D”,
“hasflyeroffer” : 0,
“hasspecialoffer” : 0,
“deliverydate” : null,
“stockfree” : 0,
“backorder” : 0,
“externalstock” : 0,
“categoryid1” : 0,
“categoryname1” : “”,
“categoryid2” : 0,
“categoryname2” : “”,
“icecatproductid” : 0,
“productimage” : “”,
“productvideo” : “”,
“longdescription” : “”,
“warrantyinfo” : “”
},

]

So i noticed the json comes back in one object if i say this right… i tried the list node but i am kind of lost… please help me out :stuck_out_tongue:

I @robert_kruizinga, any chance this is a duplicate of your other thread?

Well it kind of is… this was on self hosted… we can close this one now i guess :slight_smile: i will try your solution when i get home… i saw that the json is shown like i think it should be… too bad the woocommerce node doesnt have a batch option… i think i will have to manipulate the json then and have to use http post right? The code node only takes Java? No php?

too bad the woocommerce node doesnt have a batch option

If you have multiple items in n8n but only want to send them in batches to WooCommerce you can use the Split In Batches node for this, so there should be no need to write code :slight_smile:

Here’s a quick example workflow splitting a list 100 items in batches of 10 to demonstrate the idea:

Btw if you are self-hosting n8n this workflow might be less problematic than it is on cloud (since you’d often have more memory available, and will also be able able to monitor the memory usage and make adjustments as needed).

Hope this helps!

So here is the workflow i have now, this works fantastic for adding the products including the images, stock and prices!!

The only thing i am struggling with now is that in the woocommerce node i have an option for the categories like this:

There is a list of catgories from which i can select a name but the products (about 5000) have some different categories… It’s not workable to appoint them all manually…

When i have to put in an ID like this:

image

It says the value is not supported, Also the category ID’s that are here like “377” are different from the ID’s that Woocommerce appoints to the categories…

Any great ideas on how i can make this work?

Hi @robert_kruizinga :wave: Can you try either .toString() or .parseInt() and see if that fixes this up? It looks like it’s expecting the opposite type of what you have :thinking:

Hi @EmeraldHerald thanks for the reply… i have tried both but it doesnt work… .parseInt() gives me a value of: [undefined] for the category… with .toString i get this error:

ERROR: additionalFields.categories.map is not a function

Stack

TypeError: additionalFields.categories.map is not a function
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/WooCommerce/WooCommerce.node.js:155:71)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:649:42)
    usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:631:68

Ah, sorry about that, @robert_kruizinga - could you try using the name ( so categoryname1 adnd categoryname2 etc) instead of the ID? Or does that also produce the same error?

Hi @EmeraldHerald,

I get the same error indeed… i tried to add .toString() there as well… i can see all my categories in the list of the fixed field… but i cant put info there and i tried selecting all categories but then all products just got all categories

I saw someone on the forum who had an extra http node in the flow to update right after the creating… this didnt work for me either…

If anyone would have a solution i would be much grateful :slight_smile:

Hi @robert_kruizinga :wave: I’m not too sure - I was out the last two days, and am the only one from the support team around today, this might need a deeper look on Monday when I have a bit more availability and the brains of people like @MutedJam to bounce ideas off of :sweat_smile:

No problem i am still on holiday this week… hopefully someone has a great fix next week… enjoy the weekend

Hi @robert_kruizinga :wave: Hope you had a good weekend!

Can you try something like {{ [ $json.categoryid2 ] }} and see if that works for you as an expression? I just tried that there, and that created the product just fine on my end :slight_smile:

Hello there, i had a great weekend i hope you did as well :slight_smile:

The products are created that’s true… the stock and price all works great but the products dont get added to a category…
They all end up in the “no-category”

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.