/* ----------------------------------------------------------------------------------------- */ /* Manage cart items */ /* ----------------------------------------------------------------------------------------- */ $(document).ready(function() { /* Remove item */ $("#cart .remove input").click(function() { var orderCode = $(this).val(); $.ajax({ type: "GET", url: "/services/cart_action.php", data: "remove[]=" + orderCode, success: function() { $("#cart tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() { $(this).remove(); calcPrice(); }); }, error: function() { window.location("/services/cart_action.php?remove[]="+orderCode); } }); }); /* change quantity */ $("#cart tr .quantity input").change(function() { var orderCode = $(this).attr("name").slice(9, -1); var quantity = $(this).val(); $.ajax({ type: "GET", url: "/services/cart_action.php", data: "quantity[" + orderCode + "]=" + quantity, success: function() { var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff"; $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800); calcPrice(); }, error: function() { window.location("/services/cart_action.php?quantity[" + orderCode + "]=" + quantity); } }); }); }) function calcPrice() { var totalPrice = 0; var grandTotal = 0; $("#cart tr .quantity").parent().each(function() { var quantity = $(".quantity input", this).val(); var unitPrice = $(".unit_price", this).text().toString().replace (/\,/g, '.'); if (isNaN(unitPrice)) { var extendedPrice = 'NC'; } else { var extendedPrice = quantity*unitPrice; totalPrice += extendedPrice; } $(".extended_price", this).html(extendedPrice.toFixed(2).replace (/\./g, ',')); $("#total_price").html(totalPrice.toFixed(2).replace (/\./g, ',')); }); var shipping = $("#shipping_price").html(); grandTotal = parseFloat(totalPrice) + parseFloat(shipping); $("#grand_total_price").html(grandTotal.toFixed(2).replace (/\./g, ',')); if ( totalPrice == 0 ) { $("#cart").parent().replaceWith('

Message

Votre panier est vide.

'); } }