how to optionally sum multiple has many

I added a totalAmount computed field on a "groupOrder" which has many shopifyDraftOrder.
field on bulkOrder {
(sum(cast(draftOrders.subtotalPrice, type: "Number")) -
(customer.bulkOrderPercentDiscount ?
(sum(cast(draftOrders.subtotalPrice, type: "Number")) * (customer.bulkOrderPercentDiscount / 100))
: 0)) +
sum(cast(draftOrders.totalTax, type: "Number")) +
sum(cast(draftOrders.shippingLine.price, type: "Number"))
}
field on bulkOrder {
(sum(cast(draftOrders.subtotalPrice, type: "Number")) -
(customer.bulkOrderPercentDiscount ?
(sum(cast(draftOrders.subtotalPrice, type: "Number")) * (customer.bulkOrderPercentDiscount / 100))
: 0)) +
sum(cast(draftOrders.totalTax, type: "Number")) +
sum(cast(draftOrders.shippingLine.price, type: "Number"))
}
Is not working if either value is null/0. Suggestions?
3 Replies
Jay
JayOP7mo ago
If either the shipping or taxes is null* got it!
field on bulkOrder {
(sum(cast(draftOrders.subtotalPrice, type: "Number")) -
(customer.bulkOrderPercentDiscount ?
(sum(cast(draftOrders.subtotalPrice, type: "Number")) * (customer.bulkOrderPercentDiscount / 100))
: 0)) +
sum(cast(draftOrders.totalTax, type: "Number"), where: !isNull(draftOrders.totalTax)) +
sum(cast(draftOrders.shippingLine.price, type: "Number"), where: !isNull(draftOrders.shippingLine.price))
}
field on bulkOrder {
(sum(cast(draftOrders.subtotalPrice, type: "Number")) -
(customer.bulkOrderPercentDiscount ?
(sum(cast(draftOrders.subtotalPrice, type: "Number")) * (customer.bulkOrderPercentDiscount / 100))
: 0)) +
sum(cast(draftOrders.totalTax, type: "Number"), where: !isNull(draftOrders.totalTax)) +
sum(cast(draftOrders.shippingLine.price, type: "Number"), where: !isNull(draftOrders.shippingLine.price))
}
nope that didn't help
Chocci_Milk
Chocci_Milk7mo ago
Could you please elaborate more on the requirement of this computed field?
Jay
JayOP7mo ago
I was trying to generate a total based on the following We have a “global order” that has many draft orders. 1. The sum of all draft orders subtotal 2. Minus a custom field on the customer (decimal percent) 3. Plus the sum of drafts tax 4. Plus the sum of drafts shipping Instead of the total total always returning the proper amount, if all drafts had null or 0 for shipping, it returned 0 I don’t know why the above didn’t work, but I found a workaround

Did you find this page helpful?