bebeboutik/themes/site/js/cart-summary.js
Marion Muszynski 47965cf5ed fix go next
2017-08-17 16:03:52 +02:00

579 lines
21 KiB
JavaScript
Executable File

/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 7357 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
$(document).ready(function()
{
// If block cart isn't used, we don't bind the handle actions
if (window.ajaxCart !== undefined)
{
$('.cart_quantity_up').unbind('click').click(function(){ upQuantity($(this).attr('id').replace('cart_quantity_up_', '')); return false; });
$('.cart_quantity_down').unbind('click').click(function(){ downQuantity($(this).attr('id').replace('cart_quantity_down_', '')); return false; });
$('.cart_quantity_delete' ).unbind('click').click(function(){ deletProductFromSummary($(this).attr('id')); return false; });
$('.cart_quantity_input').typeWatch({ highlight: true, wait: 600, captureLength: 0, callback: updateQty });
$('.cart_navigation a#go_to_next_step').click(function(){ return giftMessage(true,true,false); });
$('.remove_message_gift').unbind('click').click(function(){ giftMessage(false,false,true); return false; });
}
});
function updateQty(val)
{
var id = $(this.el).attr('name');
var exp = new RegExp("^[0-9]+$");
if (exp.test(val) == true)
{
var hidden = $('input[name='+ id +'_hidden]').val();
var input = $('input[name='+ id +']').val();
var QtyToUp = parseInt(input) - parseInt(hidden);
if (parseInt(QtyToUp) > 0)
upQuantity(id.replace('quantity_', ''),QtyToUp);
else if(parseInt(QtyToUp) < 0)
downQuantity(id.replace('quantity_', ''),QtyToUp);
}
else
$('input[name='+ id +']').val($('input[name='+ id +'_hidden]').val());
}
function deletProductFromSummary(id)
{
var customizationId = 0;
var productId = 0;
var productAttributeId = 0;
var ids = 0;
ids = id.split('_');
productId = parseInt(ids[0]);
if (typeof(ids[1]) != 'undefined')
productAttributeId = parseInt(ids[1]);
if (typeof(ids[2]) != 'undefined')
customizationId = parseInt(ids[2]);
// send to GA
var product_name = $('#product_'+id+' .cart_description a.cart_product_name').text();
var qty = $('#product_'+id+' .cart_quantity input[name="quantity_'+id+'_hidden"]').val();
var category_name = $('#product_'+id).attr('category_name');
if(category_name == null || category_name == "" || category_name == undefined) {
category_name = "";
}
var prod_price = $('#product_price_'+id).text();
prod_price = prod_price.replace(",",".");
prod_price = prod_price.replace("€","");
var productToGA = {
"id" : productId,
"variant" : productAttributeId,
"name" : ""+product_name.trim()+"",
"brand": ""+category_name.trim()+"",
"category": ""+category_name.trim()+"",
"price" : prod_price.trim(),
"qty" : qty
};
$.ajax({
type: 'GET',
url: baseDir + 'cart.php',
async: true,
cache: false,
dataType: 'json',
data: 'ajax=true&delete&summary&id_product='+productId+'&ipa='+productAttributeId+ ( (customizationId != 0) ? '&id_customization='+customizationId : '') + '&token=' + static_token ,
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(error in jsonData.errors)
//IE6 bug fix
if(error != 'indexOf')
errors += jsonData.errors[error] + "\n";
}
else
{
if (parseInt(jsonData.summary.products.length) == 0)
{
$('#center_column').children().each(function() {
if ($(this).attr('id') != 'emptyCartWarning' && $(this).attr('class') != 'breadcrumb' && $(this).attr('id') != 'cart_title')
{
$(this).fadeOut('slow', function () {
$(this).remove();
});
}
});
$('#summary_products_label').remove();
$('#emptyCartWarning').fadeIn('slow');
}
else
{
$('#product_'+ id).fadeOut('slow', function() {
$(this).remove();
updateLayerDelay();
});
var exist = false;
for (i=0;i<jsonData.summary.products.length;i++)
if (jsonData.summary.products[i].id_product == productId)
exist = true;
// if all customization remove => delete product line
if (!exist)
$('#product_'+ productId+'_'+productAttributeId).fadeOut('slow', function() {
$(this).remove();
});
}
updateCartSummary(jsonData.summary);
updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
updateCustomizedDatas(jsonData.customizedDatas);
if (jsonData.carriers != null)
updateCarrierList(jsonData);
if (window.ga && ga.create){
removeFromCartToGA(productToGA);
}
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
});
}
function updateLayerDelay()
{
$('.delivery_delay_cart').each(function(index){
var line = $(this).next('tr').next('tr');
if (!$(line).hasClass('cart_item')) {
$(this).next('tr').hide();
$(this).hide();
}
});
}
function upQuantity(id, qty)
{
if(typeof(qty)=='undefined' || !qty)
qty = 1;
var customizationId = 0;
var productId = 0;
var productAttributeId = 0;
var ids = 0;
ids = id.split('_');
productId = parseInt(ids[0]);
if (typeof(ids[1]) != 'undefined')
productAttributeId = parseInt(ids[1]);
if (typeof(ids[2]) != 'undefined')
customizationId = parseInt(ids[2]);
// send to GA
var product_name = $('#product_'+id+' .cart_description a.cart_product_name').text();
var category_name = $('#product_'+id).attr('category_name');
if(category_name == null || category_name == "" || category_name == undefined) {
category_name = "";
}
var prod_price = $('#product_price_'+id).text();
prod_price = prod_price.replace(",",".");
prod_price = prod_price.replace("€","");
var productToGA = {
"id" : productId,
"variant" : productAttributeId,
"name" : ""+product_name.trim()+"",
"brand": ""+category_name.trim()+"",
"category": ""+category_name.trim()+"",
"price" : prod_price.trim(),
"qty" : qty
};
$.ajax({
type: 'GET',
url: baseDir + 'cart.php',
async: true,
cache: false,
dataType: 'json',
data: 'ajax=true&add&summary&id_product='+productId+'&ipa='+productAttributeId + ( (customizationId != 0) ? '&id_customization='+customizationId : '') + '&qty='+qty+'&token=' + static_token ,
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(error in jsonData.errors)
//IE6 bug fix
if(error != 'indexOf')
errors += jsonData.errors[error] + "\n";
alert(errors);
$('input[name=quantity_'+ id +']').val($('input[name=quantity_'+ id +'_hidden]').val());
}
else
{
updateCustomizedDatas(jsonData.customizedDatas);
updateCartSummary(jsonData.summary);
updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
if (jsonData.carriers != null)
updateCarrierList(jsonData);
// send add product to cart information to GA
if (window.ga && ga.create){
addToCartToGA(productToGA);
}
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
});
}
function downQuantity(id, qty)
{
var val = $('input[name=quantity_'+id+']').val();
var newVal = val;
if(typeof(qty)=='undefined' || !qty)
{
qty = 1;
newVal = val - 1;
}
else if (qty < 0)
qty = -qty;
var customizationId = 0;
var productId = 0;
var productAttributeId = 0;
var ids = 0;
if (newVal > 0)
{
ids = id.split('_');
productId = parseInt(ids[0]);
if (typeof(ids[1]) != 'undefined')
productAttributeId = parseInt(ids[1]);
if (typeof(ids[2]) != 'undefined')
customizationId = parseInt(ids[2]);
// send to GA
var product_name = $('#product_'+id+' .cart_description a.cart_product_name').text();
var category_name = $('#product_'+id).attr('category_name');
if(category_name == null || category_name == "" || category_name == undefined) {
category_name = "";
}
var prod_price = $('#product_price_'+id).text();
prod_price = prod_price.replace(",",".");
prod_price = prod_price.replace("€","");
var productToGA = {
"id" : productId,
"variant" : productAttributeId,
"name" : ""+product_name.trim()+"",
"brand": ""+category_name.trim()+"",
"category": ""+category_name.trim()+"",
"price" : prod_price.trim(),
"qty" : qty
};
$.ajax({
type: 'GET',
url: baseDir + 'cart.php',
async: true,
cache: false,
dataType: 'json',
data: 'ajax=true&add&summary&id_product='+productId+'&ipa='+productAttributeId+'&op=down' + ( (customizationId != 0) ? '&id_customization='+customizationId : '') + '&qty='+qty+'&token=' + static_token ,
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(error in jsonData.errors)
//IE6 bug fix
if(error != 'indexOf')
errors += jsonData.errors[error] + "\n";
alert(errors);
$('input[name=quantity_'+ id +']').val($('input[name=quantity_'+ id +'_hidden]').val());
}
else
{
updateCustomizedDatas(jsonData.customizedDatas);
updateCartSummary(jsonData.summary);
updateHookShoppingCart(jsonData.HOOK_SHOPPING_CART);
updateHookShoppingCartExtra(jsonData.HOOK_SHOPPING_CART_EXTRA);
if (jsonData.carriers != null)
updateCarrierList(jsonData);
// send add product to cart information to GA
if (window.ga && ga.create){
removeFromCartToGA(productToGA);
}
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save update quantity \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
});
}
else
{
deletProductFromSummary(id);
}
}
function updateCartSummary(json)
{
// Update products prices + discount
var i;
var nbrProducts = 0;
if (typeof json == 'undefined')
return;
$.each(json.products, function(key, json2) {
for (i=0;i<json2.products.length;i++)
{
key_for_blockcart = json2.products[i].id_product+'_'+json2.products[i].id_product_attribute;
if (json2.products[i].id_product_attribute == 0)
key_for_blockcart = json2.products[i].id_product;
$('#cart_block_product_'+key_for_blockcart+' span.quantity').html(json2.products[i].cart_quantity);
if (priceDisplayMethod != 0)
{
$('#cart_block_product_'+key_for_blockcart+' span.price').html(formatCurrency(json2.products[i].total, currencyFormat, currencySign, currencyBlank));
$('#product_price_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute).html(formatCurrency(json2.products[i].price, currencyFormat, currencySign, currencyBlank));
$('#total_product_price_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute).html(formatCurrency(json2.products[i].total, currencyFormat, currencySign, currencyBlank));
}
else
{
$('#cart_block_product_'+key_for_blockcart+' span.price').html(formatCurrency(json2.products[i].total_wt, currencyFormat, currencySign, currencyBlank));
$('#product_price_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute).html(formatCurrency(json2.products[i].price_wt, currencyFormat, currencySign, currencyBlank));
$('#total_product_price_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute).html(formatCurrency(json2.products[i].total_wt, currencyFormat, currencySign, currencyBlank));
}
nbrProducts += parseInt(json2.products[i].cart_quantity);
if(json2.products[i].id_customization == null)
{
$('input[name=quantity_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute+(json2.products[i].id_customization != null ? '_'+json2.products[i].id_customization : '')+']').val(json2.products[i].cart_quantity);
$('input[name=quantity_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute+(json2.products[i].id_customization != null ? '_'+json2.products[i].id_customization : '')+'_hidden]').val(json2.products[i].cart_quantity);
}
else
{
$('#cart_quantity_custom_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute).html(json2.products[i].cart_quantity);
}
// Show / hide quantity button if minimal quantity
if (parseInt(json2.products[i].minimal_quantity) == parseInt(json2.products[i].cart_quantity) && json2.products[i].minimal_quantity != 1)
$('#cart_quantity_down_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute+(json2.products[i].id_customization != null ? '_'+json2.products[i].id_customization : '')).fadeTo('slow',0.3);
else
$('#cart_quantity_down_'+json2.products[i].id_product+'_'+json2.products[i].id_product_attribute+(json2.products[i].id_customization != null ? '_'+json2.products[i].id_customization : '')).fadeTo('slow',1);
}
});
// Update discounts
if (json.discounts.length == 0)
{
$('.cart_discount').each(function(){$(this).remove()});
$('.cart_total_voucher').remove();
}
else
{
if (priceDisplayMethod != 0)
$('#total_discount').html(formatCurrency(json.total_discounts_tax_exc, currencyFormat, currencySign, currencyBlank));
else
$('#total_discount').html(formatCurrency(json.total_discounts, currencyFormat, currencySign, currencyBlank));
$('.cart_discount').each(function(){
var idElmt = $(this).attr('id').replace('cart_discount_','');
var toDelete = true;
for (i=0;i<json.discounts.length;i++)
{
if (json.discounts[i].id_discount == idElmt)
{
if (json.discounts[i].value_real != '!')
{
if (priceDisplayMethod != 0)
$('#cart_discount_' + idElmt + ' td.cart_discount_price span.price-discount').html(formatCurrency(json.discounts[i].value_tax_exc * -1, currencyFormat, currencySign, currencyBlank));
else
$('#cart_discount_' + idElmt + ' td.cart_discount_price span.price-discount').html(formatCurrency(json.discounts[i].value_real * -1, currencyFormat, currencySign, currencyBlank));
}
toDelete = false;
}
}
if (toDelete)
$('#cart_discount_' + idElmt + ', #cart_total_voucher').fadeTo('fast', 0, function(){ $(this).remove(); });
});
}
// Block cart
if (priceDisplayMethod != 0)
{
$('#cart_block_shipping_cost').html(formatCurrency(json.total_shipping_tax_exc, currencyFormat, currencySign, currencyBlank));
$('#cart_block_wrapping_cost').html(formatCurrency(json.total_wrapping_tax_exc, currencyFormat, currencySign, currencyBlank));
$('#cart_block_total').html(formatCurrency(json.total_price_without_tax, currencyFormat, currencySign, currencyBlank));
} else {
$('#cart_block_shipping_cost').html(formatCurrency(json.total_shipping, currencyFormat, currencySign, currencyBlank));
$('#cart_block_wrapping_cost').html(formatCurrency(json.total_wrapping, currencyFormat, currencySign, currencyBlank));
$('#cart_block_total').html(formatCurrency(json.total_price, currencyFormat, currencySign, currencyBlank));
}
$('#cart_block_tax_cost').html(formatCurrency(json.total_tax, currencyFormat, currencySign, currencyBlank));
$('.ajax_cart_quantity').html(nbrProducts);
// Cart summary
$('#summary_products_quantity').html(nbrProducts+' '+(nbrProducts > 1 ? txtProducts : txtProduct));
if (priceDisplayMethod != 0)
$('#total_product').html(formatCurrency(json.total_products, currencyFormat, currencySign, currencyBlank));
else
$('#total_product').html(formatCurrency(json.total_products_wt, currencyFormat, currencySign, currencyBlank));
$('#total_price').html(formatCurrency(json.total_price, currencyFormat, currencySign, currencyBlank));
$('#total_price_without_tax').html(formatCurrency(json.total_price_without_tax, currencyFormat, currencySign, currencyBlank));
$('#total_tax').html(formatCurrency(json.total_tax, currencyFormat, currencySign, currencyBlank));
if (json.total_shipping <= 0)
$('.cart_total_delivery').fadeOut();
else
{
$('.cart_total_delivery').fadeIn();
if (priceDisplayMethod != 0)
{
$('#total_shipping').html(formatCurrency(json.total_shipping_tax_exc, currencyFormat, currencySign, currencyBlank));
}
else
{
$('#total_shipping').html(formatCurrency(json.total_shipping, currencyFormat, currencySign, currencyBlank));
}
}
if (json.free_ship > 0 && !json.is_virtual_cart)
{
$('.cart_free_shipping').fadeIn();
$('#free_shipping').html(formatCurrency(json.free_ship, currencyFormat, currencySign, currencyBlank));
}
else
$('.cart_free_shipping').hide();
if (json.total_wrapping > 0)
{
$('#total_wrapping').html(formatCurrency(json.total_wrapping, currencyFormat, currencySign, currencyBlank));
$('#total_wrapping').parent().show();
}
else
{
$('#total_wrapping').html(formatCurrency(json.total_wrapping, currencyFormat, currencySign, currencyBlank));
$('#total_wrapping').parent().hide();
}
if (window.ajaxCart !== undefined)
ajaxCart.refresh();
}
function giftMessage(add, next, all){
var async = true;
var got_next = false;
if(next==true){
got_next = true;
}
var action = "add_message";
if(add == false){
if(all == true || !$('input#gift').is(':checked')) {
action = "remove_all";
} else {
action = "remove_message";
}
}
if(add == true){
if(!$('input#gift').is(':checked')){
action = "remove_all";
} else {
action = "add_message";
async = false; // jquery 1.4.4, we have to wait for the response before going to next step
}
}
$.ajax({
type: 'POST',
url: baseDir + 'cart.php?giftmessage=1',
async: async,
cache: false,
dataType: 'json',
data: {
'ajax':true,
'action': action,
'message': $('#gift_message').val(),
'token': static_token
},
success: function(jsonData)
{
if (jsonData.hasError)
{
var errors = '';
for(error in jsonData.errors){
//IE6 bug fix
if(error != 'indexOf'){
errors += jsonData.errors[error] + "\n";
}
}
alert(errors);
got_next = false;
}
else
{
if(jsonData.action == "add_message") {
message = escape($('#gift_message').val())
$('#my_gift_message').html($('#gift_message').val().replace(/\n/g, "<br/>"));
$('#my_gift').show();
if($('#gift_message').val()==''){
$('#my_gift').hide();
}
} else if (jsonData.action == "remove_all") {
$('#my_gift_message').html('');
$('#my_gift').hide();
$('#gift_message').val('');
$('#gift_div').hide();
$('#gift').removeAttr('checked');
} else if (jsonData.action == "remove_message") {
$('#my_gift_message').html('');
$('#my_gift').hide();
$('#gift_message').val('');
}
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
got_next = false;
console.log("ERROR\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus + "\n" + 'errorThrown: ' + errorThrown);
}
});
return got_next;
}
function updateCustomizedDatas(json)
{
for(i in json)
for(j in json[i])
for(k in json[i][j])
{
$('input[name=quantity_'+i+'_'+j+'_'+k+'_hidden]').val(json[i][j][k]['quantity']);
$('input[name=quantity_'+i+'_'+j+'_'+k+']').val(json[i][j][k]['quantity']);
}
}
function updateHookShoppingCart(html)
{
$('#HOOK_SHOPPING_CART').html(html);
}
function updateHookShoppingCartExtra(html)
{
$('#HOOK_SHOPPING_CART_EXTRA').html(html);
}