Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
Applying promotions for a shoppin cart in react in the right order

I just wanna know what is the best data structure type or any idea for executing multiple promo codes for discounts in the right order. For example I may have $164.99 worth of items in my shopping basket. After I apply promo code that takes 5% off the initial price and then I apply -20 dollars promo code I get 136.74 as the final price. Now let's say I wanna switch the order promotions have been added to the basket. On the initial price of $164.99 I first want to apply -20 dollars promo code and then 5% promo code and I get the 137.74 as the final price. How can I make it always have the right price in this case it would be $136.74 let's say.

I've only applied one switch case for that and here it is :

export const priceAfterDiscount = (cart, promotions) => {
let totalReturnedPrice = getCartTotal(cart);
promotions.forEach(element => {
    switch(element){
    case "5%OFF":
        var reducedPrice = (totalReturnedPrice * 5) / 100;
        totalReturnedPrice -= reducedPrice;
        break;
    case "20EUROFF":
        totalReturnedPrice -= 20;
        break;
    case "20%OFF":
        var reducedPrice = (totalReturnedPrice * 20) / 100;
        totalReturnedPrice -= reducedPrice;
        break;
    }
});
return totalReturnedPrice;
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

All you can do is to apply every discount to the original value.

-say 5% applied on $164.99. so discount will be of $8.2495, you need to save this value

second discount is of $20 so total discount becomes $(20+8.2495) $164.99-28.2495

Hence the discount remained same irrespective to the order of promo

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda