Hi everyone,
I am running a self-hosted n8n instance via easypanel (version 2.3.2). I am building a workflow to calculate pricing for an e-commerce platform (Mercado Livre), but I hit a roadblock.
1. The Problem I have a complex Python script that calculates the ideal selling price by iterating through different shipping cost ranges (matrix of 22 weights x 8 price ranges) to find a specific profit margin.
When I try to run it in the Code Node, I get this error: Error: Python runner unavailable: Python 3 is missing from this system
I understand I probably need to build a custom Docker image to include Python, but I would prefer to solve this using JavaScript (native Code Node) to keep my setup simple.
2. What I need I tried converting my logic to JavaScript, but I am struggling with data parsing (Brazilian number formats like 1.200,50 or 8 400 with spaces) and the iterative logic structure.
The Logic I need to implement:
-
Input: I receive a list of items with
Cost,Weight,Taxes, etc. -
Freight Matrix: I have a constant array of freight costs (22 weight rows x 8 price columns).
-
Simulation Loop:
-
For each product, I need to test different “Price Ceilings” (e.g., 29.99, 49.99, 79.99…).
-
If I sell at that ceiling, using the corresponding freight, do I achieve a 15% margin?
-
If yes, calculate the exact price.
-
If no, try the next (more expensive) bracket.
-
3. Sample Input Data (JSON)
JSON
[
{
"SKU": "322",
"SELL_IN": "133,14",
"PESO_EM_G": "8 400,00",
"Packing": "4,5",
"ComissĂŁo": "0,115",
"Imposto": "0,045",
"TAXALAWEB": "0,1",
"ACOS": "0,1"
}
]
4. My current Python Logic (which works logic-wise but not in n8n) Could someone help me translate this logic into a robust JavaScript Code Node that handles the number parsing correctly?
Python
# Pseudo-code of what I'm trying to do
def calculate(item):
# 1. Parse numbers (remove spaces, swap comma for dot)
weight = parse_br_number(item.weight)
cost = parse_br_number(item.cost)
# 2. Get Freight List based on Weight
freight_list = get_freight_row(weight) # Returns array of 8 costs
# 3. Iterative Check
price_ceilings = [29.99, 49.99, 79.99, ...]
for i, ceiling in enumerate(price_ceilings):
freight = freight_list[i]
# Check margin
margin = (ceiling - cost - freight - taxes) / ceiling
if margin >= 0.15:
# Calculate exact price
final_price = (cost + freight) / (1 - taxes - 0.15)
return final_price
return fallback_price
Thanks in advance for any help!