Nested Aggregation Problem

I’m running into a problem with trying to develop some functionality. The basic layout is as such:
User has a “shopping cart” of sorts, where they can add items from different stores. The shopping cart is represented as a list of “CartItems”. Each cartitem has a quantity and links to a product, which has its own price. Each CartItem is also tied to a store, and a user can add items to their cart from different stores.

Here’s where it gets difficult: I’d like to calculate the tax for the cart. I’m using the TaxJar API via a connector, which has you provide “to” and “from” addresses. This means that I actually can’t just make one API call to calculate tax, I have to make one API call per STORE that the user has items in their cart from, since the store’s addresses are different.

Essentially I want a dynamic list that, on the fly (rather than on button press), does the following:

  1. Groups the cart by store, calculates the sum of the product of each CartItems’ quantity and product’s price, then returns a list of these sums, along with their associated store
  2. Makes an API call for each item in the list based on its store address and sum, and returns the total value with tax
  3. Sums the final taxed values together for a total with tax included

Unfortunately, even using groups and the “ListItemExpression” value of the toolbox plugin, I can’t seem to figure out how to do this. Does anyone have any insight?

You can use List Shifter for this.

I wanted to comment and let anyone know who is reading this that @keith 's plugin above worked to solve the problem. In particular, I used the “iterate” functionality on “cart’s Stores:unique elements”. For each iteration item, I performed an API call based on the store’s location and then updated cart:filtered(store = This ListShifterKW’s CurrentIterationItem). I’d recommend anyone who this helped go support his plugin (thank you Keith!).

Another possible way you could do this is by scheduling a backend workflow on a list (the list of stores), but I haven’t explored this fully. It’s likely that this wouldn’t work for a time-sensitive application such as calculating tax on the fly, however, since there is a delay between requests.

1 Like

Yep, you’re doing it right! And you’re correct that doing this in the page is much faster than doing an API (backend) workflow on a list or scheduling a recursive workflow.

(For a talky example of how one might do that, you could check out my List Shifter video about shopping carts which talks about List Shifter and related backend plugin List Popper.)