JezK
Edit File: cart.php
<?php include "header.php"; include 'App/checkSession.php'; $userId = $_SESSION['id']; ?> <section class="dark-bg"> <h1 class="text-white">Cart</h1> </section> <section class="py-4"> <div class="container"> <span> <a style="color:#00adef" href=" <?php echo $baseUrl ?>home.php">« Back to Home</a></span> </div> </section> <div class="container"> <!-- Products --> <section class="pb-5"> <div class="d-flex justify-content-between "> <h4 class="card-title">Products</h4> <a href=" <?php echo $baseUrl ?>products.php" class="buy-btn">Add</a> </div> <div class="card py-4 mt-4"> <div class="col-lg-12"> <div class="row" id="appendCartProducts"> </div> </div> </div> </section> <!-- services --> <section class="pb-5"> <div class="d-flex justify-content-between "> <h4 class="card-title">Services</h4> <a href=" <?php echo $baseUrl ?>services.php" class="buy-btn">Add</a> </div> <div class="card py-4 mt-4"> <div class="col-lg-12"> <div class="row" id="appendCartServices"> <div class='col-lg-3 col-12 cart-card'> <img src="image 11.png" class="d-block m-auto" alt="images"> <img src="cancelImage.png" alt="cancel" class="cancelimage"> <h5 class="pd-title">Tire with Brake Unit</h5> <p class="pd-description ">Vestibulum tortor risus, rutrum at congue sed ultricies finibus.</p> <p class="product-price">RS. 2500</p> </div> </div> </div> </div> </section> <!-- Care Clubs --> <section class="pb-5"> <div class="d-flex justify-content-between "> <h4 class="card-title">Care Club</h4> <a href=" <?php echo $baseUrl ?>careclub.php" class="buy-btn">Add</a> </div> <div class="card py-4 mt-4"> <div class="col-lg-12"> <div class="row" id="appendCartCareClubs"> <div class='col-lg-3 col-12 cart-card'> <img src="image 11.png" class="d-block m-auto" alt="images"> <img src="cancelImage.png" alt="cancel" class="cancelimage"> <h5 class="pd-title">Tire with Brake Unit</h5> <p class="pd-description ">Vestibulum tortor risus, rutrum at congue sed ultricies finibus.</p> <p class="product-price">RS. 2500</p> </div> </div> </div> </div> </section> <!-- bill payment --> <div> <p class="text-center" id="emptyCartMsg" style="display: none;">Your cart is empty.<br>Add items to your cart.</p> </div> <section style="display: none;" id="billCardSection"> <div class="card bill-card"> <div id="userCartItemNames"> <!-- <div class="d-flex justify-content-between mx-4"> <div> <p class="bill-description">3. Car Baby Seat (2)</p> <p class="warranty text-left">(5 Months warranty)</p> </div> <div> <h1 class="bill-description">2500rs</h1> </div> </div> --> </div> <span class="text-center px-3 hr-line"> </span> <div class="p-4"> <div class="d-flex justify-content-between "> <div class="pd-title">Total</div> <div class="pd-title"><span id="totalAmount"></span> rs</div> </div> <div class="mt-2 fw-bold">Coupon</div> <div class="d-flex justify-content-around mt-3 "> <input type="text" name="coupon" id="coupon" class="form-control" style="width: 110px; margin-right: 0.5rem; margin-bottom: 0.5rem;" /> <button class="btn btn-primary" onclick="checkButton()" style="margin-left: 0.5rem; margin-bottom: 0.5rem;">Check</button> <p class="mt-1" style="color: #FF3E1D; vertical-align: text-top; margin-top: 0.9rem; padding-right: 90px" id="couponUsersLeft"></p> <p class="pd-title" style="vertical-align: text-top;"><span id="couponDiscountAmount"></span></p> </div> <div class="text-center px-3 hr-line"> </div> <div class="d-flex justify-content-between mt-3 "> <p class="pd-title">Tax (18%)</p> <p class="pd-title"><span id="taxAmount"></span> rs</p> </div> <div class="d-flex justify-content-between mt-3"> <p class="pd-title">Grand Total</p> <p class="pd-title"><span id="grandTotalAmount"></span> rs</p> </div> <div class="text-center px-3 mt-5"> <button class="paymentbtn" id = 'paymentbtn'>payment</button> </div> </div> </div> </section> </div> <?php include "footer.php" ?> <script> let userId = <?php echo $userId; ?>; let total; let grandTotal; let coupon_id; $(document).ready(function() { getCartProducts() getCartServices() getCartCareClubs() getUserCartDetails() }) function getCartProducts() { $.ajax({ url: 'App/cart.php', type: 'POST', data: { getCartProducts: 'getCartProducts', userId }, success: function(response) { $('#appendCartProducts').html(response) } }) } function getCartServices() { $.ajax({ url: 'App/cart.php', type: 'POST', data: { getCartServices: 'getCartServices', userId }, success: function(response) { $('#appendCartServices').html(response) } }) } function getCartCareClubs() { $.ajax({ url: 'App/cart.php', type: 'POST', data: { getCartCareClubs: 'getCartCareClubs', userId }, success: function(response) { $('#appendCartCareClubs').html(response) } }) } function getUserCartDetails() { $.ajax({ url: 'App/cart.php', type: 'POST', data: { getUserCartDetails: 'getUserCartDetails', userId }, success: function(response) { let data = JSON.parse(response) $('#userCartItemNames').html(data.htmlText); total = parseInt(data.total); $('#totalAmount').html(data.total); let totalCost = parseInt(data.total); let tax = (totalCost * 18) / 100; grandTotal = totalCost + tax; $('#taxAmount').html(tax.toFixed(2)); $('#grandTotalAmount').html(Math.round(grandTotal)); if(total == 0){ $('#emptyCartMsg').show(); $('#billCardSection').hide(); }else{ $('#emptyCartMsg').hide(); $('#billCardSection').show(); } } }) } function changeProductCount(cartId, type) { $.ajax({ url: 'App/cart.php', type: 'POST', data: { changeProductCount: 'changeProductCount', cartId, type }, success: function(response) { toastr.options.timeOut = 500; if (response == 2) { toastr.warning('Product Must Be Need Atleast 1') } getCartProducts() getUserCartDetails() } }) } function deleteCartItem(cartId) { $.ajax({ url: 'App/cart.php', type: 'POST', data: { deleteCartItem: 'deleteCartItem', cartId }, success: function(response) { toastr.options.timeOut = 500; if (response == 1) { toastr.success('Item Removed From Cart') } getCartProducts() getCartServices() getCartCareClubs() getUserCartDetails() // checkButton() } }) } $(document).ready(function() { $('#coupon').keyup(function() { let couponValue = $(this).val(); if (couponValue == '') { grandTotal = parseFloat($('#totalAmount').html()); let tax = (grandTotal * 18) / 100; grandTotal = grandTotal + tax; coupon_id = ''; $('#couponDiscountAmount').html(''); $('#couponUsersLeft').html(''); $('#taxAmount').html(tax.toFixed(2)); $('#grandTotalAmount').html(grandTotal.toFixed(2)); } }) }) function checkButton() { let couponValue = $('#coupon').val(); $.ajax({ url: 'App/cart.php', type: 'POST', data: { couponCheck: 'couponCheck', couponValue, userId }, success: function(response) { let data = JSON.parse(response) let status = data.status toastr.options.timeOut = 500; if (status == 0) { toastr.warning('Coupon Users Are Filled') } if (status == 1) { toastr.warning('Cart Amount Less Than Coupon Required Bill') } if (status == 2) { toastr.warning('Invalid Coupon') } if (status == 3) { let usersLeft = data.usersLeft; let couponAmount = data.couponAmount // grandTotal = data.grandTotal; coupon_id = data.couponData.coupon_id; $('#couponUsersLeft').html(`${usersLeft} Users Left`) $('#couponDiscountAmount').html('+ '+couponAmount+'RS') total = total - couponAmount; let tax = (total * 18) / 100; grandTotal = total + tax; $('#taxAmount').html(tax.toFixed(2)); $('#grandTotalAmount').html(grandTotal.toFixed(2)); } } }) } $('.paymentbtn').click(function() { // Get the grand total amount let grandTotalAmount = Math.round(grandTotal); if (grandTotalAmount <= 0) { toastr.warning('Please select atleast one product,service or care club') } else { $.ajax({ url: 'App/storeSession.php', type: 'POST', data: { start_payment : 'start_payment', amount: grandTotalAmount, coupon_id: coupon_id }, success: function(response) { console.log(response) if (response == 'success') { window.location.href = 'payment.php'; } } }) } }); </script>