How to map multiple items dynamically?

Describe the problem/error/question

I want to create an InvoiceNinja invoice from a WooCommerce order and my issue is how I can dynamically map all WooCommerce order line items to InvoiceNinja invoice items. Of course I can manually map line item 1 to invoice item 1 and so on. See screenshot.

However I feel there should be a dynamic way to do that. Thanks for help.

What is the error message (if any)?

There’s no error message.

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.45.1
  • Database (default: SQLite): PostgreSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own, main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: Self-hosted n8n on Debian 11 server

Hi @nadine

Thanks for posting here and welcome to the community! :raised_hands:

If you have an array in the previous node you can use the Split-Out Node to separate the fields you need into iterable items. Then you can reference them in your InvoiceNinja node.

I made a little demo workflow to illustrate:

Notice how you can preview the individual items with these little arrows:
image

Let me know if this helps! :raised_hands:

Thanks for getting back to me @ria :pray: I still have two issues.

  1. This node is currently not installed
    When I import your workflow into my (self-hosted) n8n instance (version 1.45.1), there is this


    Was the ‘manual’ node introduced in a more recent version? Anyway, I plan to update my n8n instance to version 1.50.1 in the next days.

  2. No preview from “WC order created” node
    Unfortunately, as you can see in my screenshot from my initial post, the expressions for fields “Cost” and “Service” are red. I suppose that this is because my “WC order created” node is 4 nodes back. Is that a known bug? Opposed to that I can preview the expression from the (directly) previous node. See screenshot.

Thanks again for the help.

Hi @nadine

the ‘WooCommerce Input’ node was just to simulate your WooCommerce node. All you need to do is to insert a Split-Out node in between your WC node and InvoiceNinja node and then reference it accordingly.

Would you mind sharing your workflow with some pinned (fake) data? I can try to fix it for you.

Hi @ria ,

Thanks for your feedback. Today, I updated n8n and the issues 1. and 2. from my previous post are resolved now. So, I could successfully import your workflow.

However, this doesn’t work as expected. Instead of Invoice Ninja creating 1 invoice with 6 invoice items, it creates 6 invoices with 1 item each. See screenshot.

Can you look into that?

Hello again,

as I haven’t heard back yet, I manually mapped every single WC line item to an Invoice line item which is a bit tedious.

Line Item 1 (Index 0)

Line Item 2 (Index 1)

Line Item 3 (Index 2)

For this moment it’s ok as I only have 3 different products in my shop atm. But soon, it’ll be more. So, then I really need a dynamic solution.

Also, a drawback of this manual workaround is that the node always creates an invoice with 3 items. Even if there is just 1 item you can see the additional lines with empty line items which isn’t visually appealing. See the last two empty lines in the screenshot.
image

So, could you kindly escalate this matter to the team @ria ? Thanks a lot :pray:

Hi @nadine

sorry for the late reply!

Looking at your screenshots, you should be able to use the Split Out node for the line_items and then reference the item properties individually via $json.name etc.

image

Hi @ria,
the Split out node, as you suggested it previously, does not work as intended because it creates 1 invoice per line item (which makes me end up with 3 invoices for 3 line items) and not 1 invoice with 3 line items. Can you please check this again? It’s also reproducible with the example workflow you shared. Thank you.

Hey @nadine,

I think the best way to solve this is by

  1. using a Code node to map the line items from WooCommerce to the line item structure that InvoiceNinja expects
  2. using a HTTP request node to create the invoice instead of the InvoiceNinja-specific one, passing in the line items you created in the Code node.

You would be calling this endpoint in the InvoiceNinja Rest API:

https://api-docs.invoicing.co/#post-/api/v1/invoices

You can ask ChatGPT (would recommend GPT4o) to create the code for the Code node for you with a prompt similar to this:

Please create javascript code that takes a list of objects as input and creates a list of objects as given below:

### INPUT ###
[ 
 { "name": "Product 1",
   "subtotal" : 1.20
   ...
}
]

### OUTPUT ###
[ 
 { "name": "Product 1",
   "cost" : 1.20
   ...
}
]

best,

Jon

1 Like

Hi @automaze.me ,
Thank you for your suggestion. Yes, I could do that. For now I’m living with the empty lines in the invoice.

However, in the long term from my point of view it should be possible to pass an array of line items dynamically to the InvoiceNinja node’s line items. So, I think this would be a feature request for the InvoiceNinja node. Could you pass that to the team @ria ?

Best
Nadine

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