How can I get the checkboxes to update the price and register when added to the cart in Webflow using jQuery or any other method?

TL;DR
  • Use jQuery to update the displayed product price based on selected checkboxes with data-price attributes.  
  • Override the Add to Cart function with Webflow's cart API to pass the calculated total and selected options using a $0 base product.

To dynamically update the price based on checkbox selections and pass that price to the cart in Webflow, you’ll need custom code since Webflow’s native eCommerce system doesn’t support conditional product pricing out of the box.

Here’s how to structure a solution using jQuery and Webflow’s Custom Code Embed functionality.

---

1. Structure Your Checkboxes and Base Price

  • Create a product page or component with:
  • base price element (e.g. $49.00) using a class like .product-price.
  • One or more checkboxes representing add-ons or features, each with:
    • A data attribute like data-price="10" to hold the additional cost.
    • A class like .addon-checkbox.

  • Example:
  • Base Product ($49)
  • Checkbox A: Extra Warranty (data-price="10")
  • Checkbox B: Gift Wrap (data-price="5")

---

2. Insert jQuery to Update the Price Display

  • Go to Page Settings > Custom Code > Before </body> tag.
  • Paste this script (making sure jQuery is also loaded):

<script>
  $(document).ready(function () {
    const basePrice = 49; // replace with your base price
    const $priceDisplay = $('.product-price');

    function updatePrice() {
      let total = basePrice;
      $('.addon-checkbox:checked').each(function () {
        total += parseFloat($(this).data('price'));
      });
      $priceDisplay.text(`$${total.toFixed(2)}`);
    }

    $('.addon-checkbox').on('change', updatePrice);
    updatePrice(); // initial update
  });
</script>

  • Update .product-price and .addon-checkbox with the actual classes used in your Webflow project.
  • This will dynamically update the displayed price as checkboxes are toggled.

---

3. Send the Updated Price to the Webflow Cart

Webflow's eCommerce system only allows adding predefined products, so to send a dynamic price, you must:

  • Use a “Custom Product” with a fixed price of $0.
  • Gather selected options and the computed price.
  • On “Add to Cart” button click, use Webflow.push() API to send custom data.

Example (add this inside the same <script> block):

$('.add-to-cart-button').on('click', function (e) {
  e.preventDefault();

  let total = basePrice;
  let options = [];

  $('.addon-checkbox:checked').each(function () {
    let price = parseFloat($(this).data('price'));
    let label = $(this).next('label').text();
    total += price;
    options.push(label);
  });

  window.Webflow.require('cart').add({
    productId: 'YOUR-WEBFLOW-PRODUCT-ID',
    quantity: 1,
    customData: [
      { name: "Selected Options", value: options.join(', ') },
      { name: "Adjusted Price", value: `$${total.toFixed(2)}` }
    ],
    price: total * 100 // in cents
  });
});

  • Replace 'YOUR-WEBFLOW-PRODUCT-ID' with the actual product ID.
  • You must add this custom product to your Webflow Products, priced at $0 or the minimum base price.

---

4. Make Sure to Load jQuery if Not Included

  • Webflow includes jQuery by default unless you manually disable it.
  • If disabled, re-enable it in Page Settings > Advanced settings.

---

Summary

To dynamically update product price with checkboxes and reflect that in the cart:

  • Use jQuery to detect checkbox changes and update the price.
  • Override the Add to Cart behavior using Webflow’s cart.add() API with custom price and options.
  • Use a dummy product in your store to allow for dynamic pricing.

For fully dynamic pricing and product options, also consider using tools like Finsweet Attributes or integrating a third-party cart like Foxy.io if complexity increases.

Rate this answer

Other Webflow Questions