How can I calculate the total quantity and price on my Webflow form for items in a collection list?

Published on
September 22, 2023

To calculate the total quantity and price on your Webflow form for items in a collection list, you can use some custom code and JavaScript. Here's a step-by-step guide on how to accomplish this:

  1. Set Up Your Collection List:
  • In your Webflow project, create a collection list to display your items. This can be done by adding a Collection List Wrapper and linking it to your collection in the Collection Settings panel.
  • Inside the Collection List Wrapper, create elements to display the item name, quantity, price, and total.
  1. Add Form Inputs:
  • Inside the Collection List Wrapper, add form inputs for each item's quantity. You can use a number input field for this purpose. Give each input field a unique identifier, such as "quantity-{ItemID}".
  1. Add Event Listeners:
  • In your custom code, add event listeners to each quantity input field to listen for changes in value.
  • Iterate through all the inputs and attach event listeners to them using their unique identifiers.
  • When a change is detected, call a function to update the corresponding item's total.
  1. Calculate the Totals:
  • In the function called by your event listeners, calculate the item's total by multiplying its quantity with its price.
  • Locate the item's price by using the Webflow CMS API to fetch it from the collection item.
  • Update the total by targeting the appropriate element and setting its text content to the new calculated value.

Here's an example of the JavaScript code you can use to achieve this functionality:

const quantityInputs = document.querySelectorAll('input[id^="quantity-"]');quantityInputs.forEach(input => {  input.addEventListener('input', () => {    const itemId = input.id.split('-')[1];    const quantity = parseInt(input.value);    // Fetch the item's price from the Webflow CMS API using itemId and calculate the total    // Update the corresponding item's total on the page using its unique identifier  });});

Remember to replace the comment in the code snippet with the appropriate API call to fetch the item's price and update the total.

By following these steps and implementing the provided code snippet, you'll be able to calculate the total quantity and price on your Webflow form for items in a collection list.

Additional questions:

  1. How do I add a collection list to my Webflow project?
  2. What is the Webflow CMS API?
  3. Can I use conditional logic with Webflow forms to display different prices for different item selections?