From 9a0b516712cb0ef094bb7738c453bdf2bce2b939 Mon Sep 17 00:00:00 2001 From: Ben <36240171+benkyd@users.noreply.github.com> Date: Wed, 27 Apr 2022 23:58:49 +0100 Subject: [PATCH] discount dosen't get fucked anymmore Former-commit-id: 176d2681fab12d0c7f6a835923ad6230be7648b2 --- client/public/basket.mjs | 25 +++++- client/public/components/checkout.mjs | 104 ++++++++++++++++++---- client/public/components/css/checkout.css | 6 ++ 3 files changed, 117 insertions(+), 18 deletions(-) diff --git a/client/public/basket.mjs b/client/public/basket.mjs index fe80d8f..e02f6de 100644 --- a/client/public/basket.mjs +++ b/client/public/basket.mjs @@ -83,7 +83,7 @@ export function GetBasketTotal() { return basket.total; } -export async function GetBasketTotalPrice(discount = 0, type = '£', entity_type = undefined) { +export async function GetBasketTotalPrice(discount = 0, type = '£', entity = undefined) { if (localStorage.getItem('basket') === null || !localStorage.getItem('basket')) { return 0; } @@ -101,9 +101,28 @@ export async function GetBasketTotalPrice(discount = 0, type = '£', entity_type if (res.error) { return 0; } + + if (discount !== 0) { + return res.data.subtotal - GetAbsoluteBasketDiscount(discount, type, entity); + } return res.data.subtotal; } -export async function GetAbsoluteBasketDiscount(discount = 0, type = '£', entity_type = undefined) { - +export function GetAbsoluteBasketDiscount(discount = 0, type = '£', entity = undefined) { + if (localStorage.getItem('basket') === null || !localStorage.getItem('basket')) { + return; + } + const basket = JSON.parse(localStorage.getItem('basket')); + + let discountAccumulator = 0; + + for (const [key, product] of Object.entries(basket.items)) { + if (product.type === entity) { + if (type === '£') { + discountAccumulator += product.quantity * discount; + } + } + } + + return discountAccumulator; } diff --git a/client/public/components/checkout.mjs b/client/public/components/checkout.mjs index bf4b107..0c1ed68 100644 --- a/client/public/components/checkout.mjs +++ b/client/public/components/checkout.mjs @@ -17,6 +17,7 @@ class Checkout extends Component { } Render() { + console.log(this.state); return { template: /* html */`