Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
E.g. I want to add a gift for orders spending a total net of $200 on XYZ products in an order.
Tried loop but only able to get the vendor and not the value.
Solved! Go to the solution
This is an accepted solution.
Hi @Wongg,
You can do this using the Run code action. Here's some example code:
// Inputs
query {
order {
lineItems {
vendor
originalTotalSet {
shopMoney {
amount
}
}
}
}
}
// outputs
"The output of Run Code"
type Output {
"The total amount of money for the specific vendor"
totalAmount: Float!
}
// code
export default function main(input) {
// Make sure that the data you return matches the
// shape & types defined in the output schema.
const totalAmount = input.order.lineItems.reduce((total, item) => {
if (item.vendor === "foo") {
// Add the amount to the total if vendor is 'foo'
return total + item.originalTotalSet.shopMoney.amount;
}
return total;
}, 0);
return {
totalAmount,
};
}
You'll want to change the vendor name from foo to whatever your vendor is. You can then use the totalAmount variable in downstream steps.
Hope that helps!
This is an accepted solution.
Hi @Wongg,
You can do this using the Run code action. Here's some example code:
// Inputs
query {
order {
lineItems {
vendor
originalTotalSet {
shopMoney {
amount
}
}
}
}
}
// outputs
"The output of Run Code"
type Output {
"The total amount of money for the specific vendor"
totalAmount: Float!
}
// code
export default function main(input) {
// Make sure that the data you return matches the
// shape & types defined in the output schema.
const totalAmount = input.order.lineItems.reduce((total, item) => {
if (item.vendor === "foo") {
// Add the amount to the total if vendor is 'foo'
return total + item.originalTotalSet.shopMoney.amount;
}
return total;
}, 0);
return {
totalAmount,
};
}
You'll want to change the vendor name from foo to whatever your vendor is. You can then use the totalAmount variable in downstream steps.
Hope that helps!
Thank you! I tried using the code but it is showing the below error message.
Hi @Wongg,
Does your workflow have a trigger that provides an order (like Order created)? I tested it on a test shop and it worked as expected so make sure all the inputs/outputs/code are copied as is and an order is available from the trigger.
Hi @DaveMcV,
I updated the workflow and the code is running well now!
However I realised that it is calculating the line item original amount instead of the discounted amount.
May I know which part of the code/variable should I update? Thank you!
Hi @DaveMcV,
Sorry to add on, is the workflow able to retrieve and calculate the final spending after discounts are applied to the items in the order?
This is run concurrently with item auto discount and customer discount.
Those fields come from the API. I'd suggest you take a look at the API doc and see which of the fields might work for your use case. I'm pretty sure one of them will work for you: https://shopify.dev/docs/api/admin-graphql/2024-04/objects/Order
Hi @paul_n,
I updated "originalTotalSet" to "unfulfilledDiscountedTotalSet" and it is working ideally now, thank you!
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024