Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!