In the cart object, there are Menu Items. Each of these menu items has a number of attributes, but the most important of these, when it comes to price determination, are the cents amounts for both the item and the tax. In order to determine the price of the overall check, you will need to take a sum of the following:
Line item amounts are calculated as follows:
frontend_post_discount_cents - the total amount for the individual line item after discounts are taken
frontend_pre_discount_cents - the total amount for the individual line item before discounts are taken
displayed_tax_cents - the total amount for the individual line item displayed on the front end for tax
line_item_pretax_cents - the subtotal for the individual line item. Sum the prices of the menu items and the modifiers for this line item.
line_item_tax_cents - the tax amount for the item. Sum the tax of the menu items and the modifiers for this line item.
qty (quantity) - after you calculate the Line Item IDs for each of the menu item and modifier combos for each of the items in the users cart, any time you receive the same hash you should represent them using the same line item and increment the quantity on the matching line_item_id. A very basic algorithm for this is as follows:
const cartItemsForGuest = [/* an array of objects representing each item the guest added to their cart in your app*/]
// Assuming you've appended line item ids to each cart item using a copy pasted algorithm from the determining line item ids page
let lineItemsById = {}
cartItemsForGuest.forEach(cartItem => {
if (!lineItemsById[cartItem.lineItemId]) {
lineItemsById[cartItem.lineItemId] = {
..., // Define your line item here using the request payload docs
quantity: 0
}
} else {
lineItemsById[cartItem.lineItemId].quantity += 1 // Don't actually add any new line items, just increment the quantity on the matching id. The cart item is completely identical.
}
}
Then, the amounts for the cart itself are:
- subtotal_amount - the sum of each line item's line_item_pretax_cents amounts multiplied by it's quantity, plus any cart wide fees, and minus any cart wide promotional discounts
- tip_amount - the tip in cents on the entire cart
- tax_amount - the sum of each line item's line_item_tax_cents amounts multiplied by it's quantity number, determined based on the final subtotal for each cart, based on each tax rate
- fees - fees objects which will include both fees in cents and any potential fractional fees
- promo_codes - promo code objects which will include discounts in cents or fractions to be applied