Hi Team
I am unable to calculate total to my shopping cart and discount, when a user add item on the basket(this shows both incrementing and decrementing). The problem is on getting the discount and total of those item being on the basket. Currently i can only see Nan as total need some help.
PHP
// php code
$quantity) {
$_SESSION['cart'][$product_id] = $quantity;
}
$total = $_POST['total'];
echo json_encode(array('success' => true, 'total' => $total));
exit;
}
?>
// jquery code
// Get the cart from local storage
function getCart() {
var cart = localStorage.getItem('cart');
if (cart) {
return JSON.parse(cart);
} else {
return {};
}
}
// Save the cart to local storage
function saveCart(cart) {
localStorage.setItem('cart', JSON.stringify(cart));
}
// Update the cart count in the navbar
function updateCartCount() {
var cartCount = Object.values(getCart()).reduce((a, b) => a + b, 0);
$('#cart-count').text(cartCount);
}
// Add a product to the cart
function addToCart(productId, quantity) {
var cart = getCart();
if (cart.hasOwnProperty(productId)) {
var newQuantity = parseInt(quantity);
if (newQuantity ').appendTo($cart_table.find('tbody'));
$('').append($('').attr('src', 'img/detailsquare.jpg').attr('alt', product.product_name)).appendTo($row);
$('').append($('').attr('', '').text(product.product_name)).appendTo($row);
$('').text(quantity).appendTo($row);
$('').text('$' + price.toFixed(2)).appendTo($row);
$('').text((discount * 100) + '%').appendTo($row);
$('').text('$' + total.toFixed(2)).appendTo($row);
},
error: function(xhr, status, error) {
console.error('Error getting product details:', error);
}
});
}
// Update the total amount in the cart
calculate_total();
}
$(document).ready(function() {
$('.btn-plus').click(function() {
var productId = $(this).closest('.input-group').find('.product-quantity').data('product-id');
var quantityInput = $(this).closest('.input-group').find('.product-quantity');
var currentQuantity = parseInt(quantityInput.val());
var newQuantity = currentQuantity + 1;
quantityInput.val(newQuantity);
addToCart(productId, newQuantity);
});
$('.btn-minus').click(function() {
var productId = $(this).closest('.input-group').find('.product-quantity').data('product-id');
var quantityInput = $(this).closest('.input-group').find('.product-quantity');
var currentQuantity = parseInt(quantityInput.val());
if (currentQuantity > 1) {
var newQuantity = currentQuantity - 1;
quantityInput.val(newQuantity);
addToCart(productId, newQuantity);
}
});
// Initialize the cart count in the navbar
updateCartCount();
});
// html code
Shopping cart
You currently have
Product
Quantity
Unit price
Discount
Total
White Blouse Armani
-
+
R
R
White Blouse Armani
-
+
R
%13
R
Total
Continue shopping
Update cart
Proceed to checkout
Amit MohantyRank #112 · 20K reputationPosted Apr 24, 2023, 2:43 AM EDT
Check this
function calculate_total() { var cart = getCart(); var total = 0; for (var productId in cart) { var product = getProductId(productId); var discount = 0; if (isNaN(product.price)) { console.error('Invalid price for product', product); continue; } if (isNaN(product.discount)) { console.error('Invalid discount for product', product); discount = 0; } else { discount = parseFloat(product.discount); } var price = parseFloat(product.price); var quantity = parseInt(cart[productId]); var subtotal = price * quantity * (1 - discount); total += subtotal; } $('#total-amount').text('$' + total.toFixed(0)); // update the text of the total td element $('#total').val(total.toFixed(0)); return total.toFixed(0); }Guest UserRank #877 · 2.5K reputationPosted Apr 24, 2023, 5:30 AM EDT
Hi Amit and Swain, the new code you guys shared is working(data from the table inserted), but to get_product_details.php how can i display this to the shopping cart page? My requirement is to be able to display these data on the same page, question also then be if its a new data being added on the cart, how do i despose this as well?
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 24, 2023, 2:30 AM EDT
function calculate_total() { var cart = getCart(); var total = 0; for (var productId in cart) { var product = getProductId(productId); var price = parseFloat(product.price); var quantity = parseInt(cart[productId]); var discount = parseFloat(product.discount); // Check if all values are numbers before adding to total if (!isNaN(price) && !isNaN(quantity) && !isNaN(discount)) { var subtotal = price * quantity * (1 - discount); total += subtotal; } } $('#total-amount').text('$' + total.toFixed(0)); // update the text of the total td element $('#total').val(total.toFixed(0)); return total.toFixed(0); }