Calculate price of WooCommerce line item

davidw

Member
I want to create invoices in Invoice Ninja from new WooCommerce orders. Here is my workflow outline:

1. Trigger: New order in WooCommerce
2. Line itemizer to get the items in the order
3. Create an invoice in Invoice Ninja

The problem is that WooCommerce doesn't include the line item price, it only includes the subtotal. The below order includes the following:

Web Development 2x$50 = $100
Website migration 1x$75 = $75

CleanShot 2023-11-05 at 00.52.58.png


Invoice Ninja needs the rate and quantity of each line item, i.e. rate for Web Development is $50. It's easy to find this value, just take the subtotal and divide by the quantity. But how do I do this with Pabbly please?

I need the output to be 50,75.

Thank you
 

ArshilAhmad

Well-known member
Staff member
Hi @david466,

Please try adding this JavaScript code to your workflow using Code by Pabbly. This should give you the desired result.
JavaScript:
const subtotals = [100, 75]; // Array of subtotal prices
const quantities = [2, 1];  // Array of quantities

const result = subtotals.map((subtotal, index) => subtotal / quantities[index]);

const resultString = result.join(', ');

console.log(resultString); // Output the results as a comma-separated string

1699120623923.png



 

davidw

Member
Thank you, exactly what I needed. Is it best practice to simply assign the result to the output variable? e.g.

JavaScript:
const result = subtotals.map((subtotal, index) => subtotal / quantities[index]);

const resultString = result.join(', ');

output = resultString;
 

ArshilAhmad

Well-known member
Staff member
The code I shared above was generated using ChatGPT, so unfortunately, I won't be able to assist you with coding-related queries as it's beyond our scope of expertise.
 
Top