How can I create a microflow that calculates the total price of products in a Mendix shopping cart?

Scenario: An online store built with Mendix allows customers to add products to their shopping cart. The total price of the cart needs to be calculated dynamically based on the quantity and price of each product
5 answers

Here you are creating a list of carts, but it has no cart objects in it. Taking the sum of an empty list will always lead to an empty total. 



In addition, I would still recommend building up your domain model differently using line items instead of just products and carts so you can reuse products.


Imagine you have your domain model set up like this:



















There are several moments to do this, but what you'll basically want to do is:


  • Retrieve all orderlines that are in your order (shopping cart)
  • For each order line, retrieve the product.
  • Multiply the quantity the user selected with the price of the product
  • Add up all orderlines
  • Add any additional costs like VAT or shipping
  • You now have your total

Hi Muhammad,


Can you share your domain model?

I assume you will need to have a microflow which will retrieve all products and prices and then sum everything and put that value in an attribute to be shown on the page.


Can you explain in more detail what is the issue you are facing?



Inês Fragoeiro


hey, yes see my screenshots below for better understanding, mendixcart1.PNGthis is my cart page;





mendixcart3.PNGtriggerd the microflow from the add to cart button


meneixcart4.PNGthe mircoflow itself


Hi Muhammed,


If I understand you in the right way, Here are some insights that may help you:


  1. Data Model:

    • Ensure that your data model includes entities for Products, Cart, and CartItems.
    • The Cart entity should have a one-to-many relationship with CartItems.
  2. Page Design:

    • Create a page for the shopping cart where users can view and manage their selected items.
    • Include a data view or list view to display the items in the cart.
  3. Microflow for Adding Products to Cart:

    • Create a microflow that is triggered when a user adds a product to the cart.
    • In the microflow, check if the product is already in the cart. If yes, update the quantity; otherwise, create a new CartItem.
    • Make sure to update the total price of the Cart entity based on the changes in CartItems.
  4. Dynamic Total Price Calculation:

    • On the shopping cart page, use a calculated attribute or a microflow to dynamically calculate the total price of the cart.
    • This calculation should consider the quantity and price of each CartItem associated with the current Cart.
  5. Update Total Price on Changes:

    • Whenever the quantity of an item in the cart changes, or an item is added or removed, trigger the recalculation of the total price.
    • You can achieve this by using events or actions associated with user interactions or changes in the underlying data.
  6. Display Total Price:

    • Display the dynamically calculated total price prominently on the shopping cart page.

Here's a simplified example of what the microflow logic might look like:


Add To Cart:
1. Retrieve Cart based on user session or user ID.
2. Retrieve or create CartItem for the selected product.
3. Update CartItem quantity based on user input.
4. Update CartItem price based on the Product entity.
5. Update Cart total price by summing up all CartItems.

Calculate Tota lPrice:
1. Retrieve Cart based on user session or user ID.
2. Retrieve all CartItems associated with the Cart.
3. Calculate the total price by summing up (quantity * price) for each CartItem.
4. Update the Cart entity with the new total price.



** Kindly accept my answer if I assist you in solving your problem.