VEKIA

prestashop ajax-cart.js

Oct 26th, 2013
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.82 KB | None | 0 0
  1. /*
  2. * 2007-2013 PrestaShop
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * This source file is subject to the Academic Free License (AFL 3.0)
  7. * that is bundled with this package in the file LICENSE.txt.
  8. * It is also available through the world-wide-web at this URL:
  9. * http://opensource.org/licenses/afl-3.0.php
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to [email protected] so we can send you a copy immediately.
  13. *
  14. * DISCLAIMER
  15. *
  16. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  17. * versions in the future. If you wish to customize PrestaShop for your
  18. * needs please refer to http://www.prestashop.com for more information.
  19. *
  20. * @author PrestaShop SA <[email protected]>
  21. * @copyright 2007-2013 PrestaShop SA
  22. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  23. * International Registered Trademark & Property of PrestaShop SA
  24. */
  25.  
  26. // Retrocompatibility with 1.4
  27. if (typeof baseUri === "undefined" && typeof baseDir !== "undefined")
  28. baseUri = baseDir;
  29.  
  30. //JS Object : update the cart by ajax actions
  31. var ajaxCart = {
  32. nb_total_products: 0,
  33.  
  34. //override every button in the page in relation to the cart
  35. overrideButtonsInThePage : function(){
  36. //for every 'add' buttons...
  37. $('.ajax_add_to_cart_button').unbind('click').click(function(){
  38. var idProduct = $(this).attr('rel').replace('nofollow', '').replace('ajax_id_product_', '');
  39. if ($(this).attr('disabled') != 'disabled')
  40. ajaxCart.add(idProduct, null, false, this);
  41. return false;
  42. });
  43. //for product page 'add' button...
  44. $('#add_to_cart input').unbind('click').click(function(){
  45. ajaxCart.add( $('#product_page_product_id').val(), $('#idCombination').val(), true, null, $('#quantity_wanted').val(), null);
  46. return false;
  47. });
  48.  
  49. //for 'delete' buttons in the cart block...
  50. $('#cart_block_list .ajax_cart_block_remove_link').unbind('click').click(function(){
  51. // Customized product management
  52. var customizationId = 0;
  53. var productId = 0;
  54. var productAttributeId = 0;
  55. var customizableProductDiv = $($(this).parent().parent()).find("div[id^=deleteCustomizableProduct_]");
  56.  
  57. if (customizableProductDiv && $(customizableProductDiv).length)
  58. {
  59. $(customizableProductDiv).each(function(){
  60. var ids = $(this).attr('id').split('_');
  61. if (typeof(ids[1]) != 'undefined')
  62. {
  63. customizationId = parseInt(ids[1]);
  64. productId = parseInt(ids[2]);
  65. if (typeof(ids[3]) != 'undefined')
  66. productAttributeId = parseInt(ids[3]);
  67. return false;
  68. }
  69. });
  70. }
  71.  
  72. // Common product management
  73. if (!customizationId)
  74. {
  75. //retrieve idProduct and idCombination from the displayed product in the block cart
  76. var firstCut = $(this).parent().parent().attr('id').replace('cart_block_product_', '');
  77. firstCut = firstCut.replace('deleteCustomizableProduct_', '');
  78. ids = firstCut.split('_');
  79. productId = parseInt(ids[0]);
  80. if (typeof(ids[1]) != 'undefined')
  81. productAttributeId = parseInt(ids[1]);
  82. }
  83.  
  84. var idAddressDelivery = $(this).parent().parent().attr('id').match(/.*_\d+_\d+_(\d+)/)[1];
  85.  
  86. // Removing product from the cart
  87. ajaxCart.remove(productId, productAttributeId, customizationId, idAddressDelivery);
  88. return false;
  89. });
  90. },
  91.  
  92. // try to expand the cart
  93. expand : function(){
  94. if ($('#cart_block_list').hasClass('collapsed'))
  95. {
  96. $('#cart_block_summary').slideUp(200, function(){
  97. $(this).addClass('collapsed').removeClass('expanded');
  98. $('#cart_block_list').slideDown({
  99. duration: 450,
  100. complete: function(){$(this).addClass('expanded').removeClass('collapsed');}
  101. });
  102. });
  103. // toogle the button expand/collapse button
  104. $('#block_cart_expand').fadeOut('slow', function(){
  105. $('#block_cart_collapse').fadeIn('fast');
  106. });
  107.  
  108. // save the expand statut in the user cookie
  109. $.ajax({
  110. type: 'POST',
  111. headers: { "cache-control": "no-cache" },
  112. url: baseDir + 'modules/blockcart/blockcart-set-collapse.php' + '?rand=' + new Date().getTime(),
  113. async: true,
  114. cache: false,
  115. data: 'ajax_blockcart_display=expand'
  116. });
  117.  
  118.  
  119. }
  120. },
  121. // Fix display when using back and previous browsers buttons
  122. refresh : function(){
  123. $.ajax({
  124. type: 'POST',
  125. headers: { "cache-control": "no-cache" },
  126. url: baseUri + '?rand=' + new Date().getTime(),
  127. async: true,
  128. cache: false,
  129. dataType : "json",
  130. data: 'controller=cart&ajax=true&token=' + static_token,
  131. success: function(jsonData)
  132. {
  133. ajaxCart.updateCart(jsonData);
  134. }
  135. });
  136. },
  137.  
  138. // try to collapse the cart
  139. collapse : function(){
  140.  
  141. if ($('#cart_block_list').hasClass('expanded'))
  142. {
  143. $('#cart_block_list').slideUp('slow', function(){
  144. $(this).addClass('collapsed').removeClass('expanded');
  145. $('#cart_block_summary').slideDown(450, function(){
  146. $(this).addClass('expanded').removeClass('collapsed');
  147. });
  148. });
  149. $('#block_cart_collapse').fadeOut('slow', function(){
  150. $('#block_cart_expand').fadeIn('fast');
  151. });
  152.  
  153. // save the expand statut in the user cookie
  154. $.ajax({
  155. type: 'POST',
  156. headers: { "cache-control": "no-cache" },
  157. url: baseDir + 'modules/blockcart/blockcart-set-collapse.php' + '?rand=' + new Date().getTime(),
  158. async: true,
  159. cache: false,
  160. data: 'ajax_blockcart_display=collapse' + '&rand=' + new Date().getTime()
  161. });
  162. }
  163. },
  164.  
  165. // Update the cart information
  166. updateCartInformation : function (jsonData, addedFromProductPage)
  167. {
  168. ajaxCart.updateCart(jsonData);
  169.  
  170. //reactive the button when adding has finished
  171. if (addedFromProductPage)
  172. $('#add_to_cart input').removeAttr('disabled').addClass('exclusive').removeClass('exclusive_disabled');
  173. else
  174. $('.ajax_add_to_cart_button').removeAttr('disabled');
  175. },
  176.  
  177. // add a product in the cart via ajax
  178. add : function(idProduct, idCombination, addedFromProductPage, callerElement, quantity, whishlist){
  179. if (addedFromProductPage && !checkCustomizations())
  180. {
  181. alert(fieldRequired);
  182. return ;
  183. }
  184. emptyCustomizations();
  185. //disabled the button when adding to not double add if user double click
  186. if (addedFromProductPage)
  187. {
  188. $('#add_to_cart input').attr('disabled', true).removeClass('exclusive').addClass('exclusive_disabled');
  189. $('.filled').removeClass('filled');
  190. }
  191. else
  192. $(callerElement).attr('disabled', true);
  193.  
  194. if ($('#cart_block_list').hasClass('collapsed'))
  195. this.expand();
  196. //send the ajax request to the server
  197. $.ajax({
  198. type: 'POST',
  199. headers: { "cache-control": "no-cache" },
  200. url: baseUri + '?rand=' + new Date().getTime(),
  201. async: true,
  202. cache: false,
  203. dataType : "json",
  204. data: 'controller=cart&add=1&ajax=true&qty=' + ((quantity && quantity != null) ? quantity : '1') + '&id_product=' + idProduct + '&token=' + static_token + ( (parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination): ''),
  205. success: function(jsonData,textStatus,jqXHR)
  206. {
  207. // add appliance to whishlist module
  208. if (whishlist && !jsonData.errors)
  209. WishlistAddProductCart(whishlist[0], idProduct, idCombination, whishlist[1]);
  210.  
  211. // add the picture to the cart
  212. var $element = $(callerElement).parent().parent().find('a.product_image img,a.product_img_link img');
  213. if (!$element.length)
  214. $element = $('img');
  215. var $picture = $element.clone();
  216. var pictureOffsetOriginal = $element.offset();
  217. pictureOffsetOriginal.right = $(window).innerWidth() - pictureOffsetOriginal.left - $element.width();
  218. pictureOffsetOriginal.top = $(window).innerHeight() - pictureOffsetOriginal.top - $element.height();
  219.  
  220.  
  221. if ($picture.length)
  222. {
  223. $picture.css({
  224. position: 'absolute',
  225. top: pictureOffsetOriginal.top,
  226. right: pictureOffsetOriginal.right
  227. });
  228. }
  229.  
  230. var pictureOffset = $picture.offset();
  231. var cartBlock = $('#cart_block');
  232. if (!$('#cart_block')[0] || !$('#cart_block').offset().top || !$('#cart_block').offset().left)
  233. cartBlock = $('#shopping_cart');
  234. var cartBlockOffset = cartBlock.offset();
  235. cartBlockOffset.right = $(window).innerWidth() - cartBlockOffset.left - cartBlock.width();
  236.  
  237. // Check if the block cart is activated for the animation
  238. if (cartBlockOffset != undefined && $picture.length)
  239. {
  240. $picture.appendTo('body');
  241. $picture
  242. .css({
  243. position: 'absolute',
  244. top: pictureOffsetOriginal.top,
  245. right: pictureOffsetOriginal.right,
  246. zIndex: 4242
  247. })
  248. .animate({
  249. width: $element.attr('width')*0.66,
  250. height: $element.attr('height')*0.66,
  251. opacity: 0.2,
  252. top: cartBlockOffset.top + 30,
  253. right: cartBlockOffset.right + 15
  254. }, 1000)
  255. .fadeOut(100, function() {
  256. ajaxCart.updateCartInformation(jsonData, addedFromProductPage);
  257. $(this).remove();
  258. });
  259. }
  260. else
  261. ajaxCart.updateCartInformation(jsonData, addedFromProductPage);
  262. },
  263. error: function(XMLHttpRequest, textStatus, errorThrown)
  264. {
  265. alert("Impossible to add the product to the cart.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
  266. //reactive the button when adding has finished
  267. if (addedFromProductPage)
  268. $('#add_to_cart input').removeAttr('disabled').addClass('exclusive').removeClass('exclusive_disabled');
  269. else
  270. $(callerElement).removeAttr('disabled');
  271. }
  272. });
  273. },
  274.  
  275. //remove a product from the cart via ajax
  276. remove : function(idProduct, idCombination, customizationId, idAddressDelivery){
  277. //send the ajax request to the server
  278. $.ajax({
  279. type: 'POST',
  280. headers: { "cache-control": "no-cache" },
  281. url: baseUri + '?rand=' + new Date().getTime(),
  282. async: true,
  283. cache: false,
  284. dataType : "json",
  285. data: 'controller=cart&delete=1&id_product=' + idProduct + '&ipa=' + ((idCombination != null && parseInt(idCombination)) ? idCombination : '') + ((customizationId && customizationId != null) ? '&id_customization=' + customizationId : '') + '&id_address_delivery=' + idAddressDelivery + '&token=' + static_token + '&ajax=true',
  286. success: function(jsonData) {
  287. ajaxCart.updateCart(jsonData);
  288. if ($('body').attr('id') == 'order' || $('body').attr('id') == 'order-opc')
  289. deleteProductFromSummary(idProduct+'_'+idCombination+'_'+customizationId+'_'+idAddressDelivery);
  290. },
  291. error: function() {alert('ERROR: unable to delete the product');}
  292. });
  293. },
  294.  
  295. //hide the products displayed in the page but no more in the json data
  296. hideOldProducts : function(jsonData) {
  297. //delete an eventually removed product of the displayed cart (only if cart is not empty!)
  298. if ($('#cart_block_list dl.products').length > 0)
  299. {
  300. var removedProductId = null;
  301. var removedProductData = null;
  302. var removedProductDomId = null;
  303. //look for a product to delete...
  304. $('#cart_block_list dl.products dt').each(function(){
  305. //retrieve idProduct and idCombination from the displayed product in the block cart
  306. var domIdProduct = $(this).attr('id');
  307. var firstCut = domIdProduct.replace('cart_block_product_', '');
  308. var ids = firstCut.split('_');
  309.  
  310. //try to know if the current product is still in the new list
  311. var stayInTheCart = false;
  312. for (aProduct in jsonData.products)
  313. {
  314. //we've called the variable aProduct because IE6 bug if this variable is called product
  315. //if product has attributes
  316. if (jsonData.products[aProduct]['id'] == ids[0] && (!ids[1] || jsonData.products[aProduct]['idCombination'] == ids[1]))
  317. {
  318. stayInTheCart = true;
  319. // update the product customization display (when the product is still in the cart)
  320. ajaxCart.hideOldProductCustomizations(jsonData.products[aProduct], domIdProduct);
  321. }
  322. }
  323. //remove product if it's no more in the cart
  324. if (!stayInTheCart)
  325. {
  326. removedProductId = $(this).attr('id');
  327. if (removedProductId != null)
  328. {
  329. var firstCut = removedProductId.replace('cart_block_product_', '');
  330. var ids = firstCut.split('_');
  331.  
  332. $('#'+removedProductId).addClass('strike').fadeTo('slow', 0, function(){
  333. $(this).slideUp('slow', function(){
  334. $(this).remove();
  335. // If the cart is now empty, show the 'no product in the cart' message and close detail
  336. if($('#cart_block dl.products dt').length == 0)
  337. {
  338. $("#cart_block").stop(true, true).slideUp(200);
  339. $('#cart_block_no_products:hidden').slideDown(450);
  340. $('#cart_block dl.products').remove();
  341. }
  342. });
  343. });
  344. $('#cart_block_combination_of_' + ids[0] + (ids[1] ? '_'+ids[1] : '') + (ids[2] ? '_'+ids[2] : '')).fadeTo('fast', 0, function(){
  345. $(this).slideUp('fast', function(){
  346. $(this).remove();
  347. });
  348. });
  349. }
  350. }
  351. });
  352. }
  353. },
  354.  
  355. hideOldProductCustomizations : function (product, domIdProduct)
  356. {
  357. var customizationList = $('#customization_' + product['id'] + '_' + product['idCombination']);
  358. if(customizationList.length > 0)
  359. {
  360. $(customizationList).find("li").each(function(){
  361. $(this).find("div").each(function() {
  362. var customizationDiv = $(this).attr('id');
  363. var tmp = customizationDiv.replace('deleteCustomizableProduct_', '');
  364. var ids = tmp.split('_');
  365. if ((parseInt(product.idCombination) == parseInt(ids[2])) && !ajaxCart.doesCustomizationStillExist(product, ids[0]))
  366. $('#' + customizationDiv).parent().addClass('strike').fadeTo('slow', 0, function(){
  367. $(this).slideUp('slow');
  368. $(this).remove();
  369. });
  370. });
  371. });
  372. }
  373.  
  374. var removeLinks = $('#' + domIdProduct).find('.ajax_cart_block_remove_link');
  375. if (!product.hasCustomizedDatas && !removeLinks.length)
  376. $('#' + domIdProduct + ' span.remove_link').html('<a class="ajax_cart_block_remove_link" rel="nofollow" href="' + baseUri + '?controller=cart&amp;delete=1&amp;id_product=' + product['id'] + '&amp;ipa=' + product['idCombination'] + '&amp;token=' + static_token + '"> </a>');
  377. if (product.is_gift)
  378. $('#' + domIdProduct + ' span.remove_link').html('');
  379. },
  380.  
  381. doesCustomizationStillExist : function (product, customizationId)
  382. {
  383. var exists = false;
  384.  
  385. $(product.customizedDatas).each(function() {
  386. if (this.customizationId == customizationId)
  387. {
  388. exists = true;
  389. // This return does not mean that we found nothing but simply break the loop
  390. return false;
  391. }
  392. });
  393. return (exists);
  394. },
  395.  
  396. //refresh display of vouchers (needed for vouchers in % of the total)
  397. refreshVouchers : function (jsonData) {
  398. if (typeof(jsonData.discounts) == 'undefined' || jsonData.discounts.length == 0)
  399. $('#vouchers').hide();
  400. else
  401. {
  402. $('#vouchers tbody').html('');
  403. for (i=0; i < jsonData.discounts.length; i++)
  404. {
  405. if (parseFloat(jsonData.discounts[i].price_float) > 0)
  406. {
  407. var delete_link = '';
  408. if (jsonData.discounts[i].code.length)
  409. delete_link = '<a class="delete_voucher" href="'+jsonData.discounts[i].link+'" title="'+delete_txt+'"><img src="'+img_dir+'icon/delete.gif" alt="'+delete_txt+'" class="icon" /></a>';
  410. $('#vouchers tbody').append($(
  411. '<tr class="bloc_cart_voucher" id="bloc_cart_voucher_'+jsonData.discounts[i].id+'">'
  412. + '<td class="quantity">1x</td>'
  413. + '<td class="name" title="'+jsonData.discounts[i].description+'">'+jsonData.discounts[i].name+'</td>'
  414. + '<td class="price">-'+jsonData.discounts[i].price+'</td>'
  415. + '<td class="delete">' + delete_link + '</td>'
  416. + '</tr>'
  417. ));
  418. }
  419. }
  420. $('#vouchers').show();
  421. }
  422.  
  423. },
  424.  
  425. // Update product quantity
  426. updateProductQuantity : function (product, quantity) {
  427.  
  428. $('#cart_block_product_' + product.id + '_' + (product.idCombination ? product.idCombination : '0')+ '_' + (product.idAddressDelivery ? product.idAddressDelivery : '0') + ' .quantity').fadeTo('fast', 0, function() {
  429. $(this).text(quantity);
  430. $(this).fadeTo('fast', 1, function(){
  431. $(this).fadeTo('fast', 0, function(){
  432. $(this).fadeTo('fast', 1, function(){
  433. $(this).fadeTo('fast', 0, function(){
  434. $(this).fadeTo('fast', 1);
  435. });
  436. });
  437. });
  438. });
  439. });
  440. },
  441.  
  442.  
  443. //display the products witch are in json data but not already displayed
  444. displayNewProducts : function(jsonData) {
  445.  
  446. //add every new products or update displaying of every updated products
  447. $(jsonData.products).each(function(){
  448. //fix ie6 bug (one more item 'undefined' in IE6)
  449. if (this.id != undefined)
  450. {
  451. //create a container for listing the products and hide the 'no product in the cart' message (only if the cart was empty)
  452.  
  453. if ($('#cart_block dl.products').length == 0)
  454. {
  455. $('#cart_block_no_products').before('<dl class="products"></dl>');
  456. $('#cart_block_no_products').hide();
  457. }
  458. //if product is not in the displayed cart, add a new product's line
  459. var domIdProduct = this.id + '_' + (this.idCombination ? this.idCombination : '0') + '_' + (this.idAddressDelivery ? this.idAddressDelivery : '0');
  460. var domIdProductAttribute = this.id + '_' + (this.idCombination ? this.idCombination : '0');
  461. if ($('#cart_block_product_'+ domIdProduct).length == 0)
  462. {
  463. var productId = parseInt(this.id);
  464. var productAttributeId = (this.hasAttributes ? parseInt(this.attributes) : 0);
  465. var content = '<dt class="hidden" id="cart_block_product_' + domIdProduct + '">';
  466. content += '<span class="quantity-formated"><span class="quantity">' + this.quantity + '</span>x</span>';
  467. var min = this.name.indexOf(';', 10);
  468. var name = (this.name.length > 12 ? this.name.substring(0, ((min - 10) <= 7) ? min : 10) + '...' : this.name);
  469. content += '<a href="' + this.link + '" title="' + this.name + '" class="cart_block_product_name">' + name + '</a>';
  470.  
  471. if (typeof(this.is_gift) == 'undefined' || this.is_gift == 0)
  472. content += '<span class="remove_link"><a rel="nofollow" class="ajax_cart_block_remove_link" href="' + baseUri + '?controller=cart&amp;delete=1&amp;id_product=' + productId + '&amp;token=' + static_token + (this.hasAttributes ? '&amp;ipa=' + parseInt(this.idCombination) : '') + '"> </a></span>';
  473. else
  474. content += '<span class="remove_link"></span>';
  475. if (typeof(freeProductTranslation) != 'undefined')
  476. content += '<span class="price">' + (parseFloat(this.price_float) > 0 ? this.priceByLine : freeProductTranslation) + '</span>';
  477. content += '</dt>';
  478. if (this.hasAttributes)
  479. content += '<dd id="cart_block_combination_of_' + domIdProduct + '" class="hidden"><a href="' + this.link + '" title="' + this.name + '">' + this.attributes + '</a>';
  480. if (this.hasCustomizedDatas)
  481. content += ajaxCart.displayNewCustomizedDatas(this);
  482. if (this.hasAttributes) content += '</dd>';
  483.  
  484. $('#cart_block dl.products').append(content);
  485. }
  486. //else update the product's line
  487. else
  488. {
  489. var jsonProduct = this;
  490. if($.trim($('#cart_block_product_' + domIdProduct + ' .quantity').html()) != jsonProduct.quantity || $.trim($('#cart_block_product_' + domIdProduct + ' .price').html()) != jsonProduct.priceByLine)
  491. {
  492. // Usual product
  493. if (!this.is_gift)
  494. $('#cart_block_product_' + domIdProduct + ' .price').text(jsonProduct.priceByLine);
  495. else
  496. $('#cart_block_product_' + domIdProduct + ' .price').html(freeProductTranslation);
  497. ajaxCart.updateProductQuantity(jsonProduct, jsonProduct.quantity);
  498.  
  499. // Customized product
  500. if (jsonProduct.hasCustomizedDatas)
  501. {
  502. customizationFormatedDatas = ajaxCart.displayNewCustomizedDatas(jsonProduct);
  503. if (!$('#customization_' + domIdProductAttribute).length)
  504. {
  505. if (jsonProduct.hasAttributes)
  506. $('#cart_block_combination_of_' + domIdProduct).append(customizationFormatedDatas);
  507. else
  508. $('#cart_block dl.products').append(customizationFormatedDatas);
  509. }
  510. else
  511. {
  512. $('#customization_' + domIdProductAttribute).html('');
  513. $('#customization_' + domIdProductAttribute).append(customizationFormatedDatas);
  514. }
  515. }
  516. }
  517. }
  518. $('#cart_block dl.products .hidden').slideDown(450).removeClass('hidden');
  519.  
  520. var removeLinks = $('#cart_block_product_' + domIdProduct).find('a.ajax_cart_block_remove_link');
  521. if (this.hasCustomizedDatas && removeLinks.length)
  522. $(removeLinks).each(function() {
  523. $(this).remove();
  524. });
  525. }
  526. });
  527. },
  528.  
  529. displayNewCustomizedDatas : function(product)
  530. {
  531. var content = '';
  532. var productId = parseInt(product.id);
  533. var productAttributeId = typeof(product.idCombination) == 'undefined' ? 0 : parseInt(product.idCombination);
  534. var hasAlreadyCustomizations = $('#customization_' + productId + '_' + productAttributeId).length;
  535.  
  536. if (!hasAlreadyCustomizations)
  537. {
  538. if (!product.hasAttributes)
  539. content += '<dd id="cart_block_combination_of_' + productId + '" class="hidden">';
  540. if ($('#customization_' + productId + '_' + productAttributeId).val() == undefined)
  541. content += '<ul class="cart_block_customizations" id="customization_' + productId + '_' + productAttributeId + '">';
  542. }
  543.  
  544. $(product.customizedDatas).each(function()
  545. {
  546. var done = 0;
  547. customizationId = parseInt(this.customizationId);
  548. productAttributeId = typeof(product.idCombination) == 'undefined' ? 0 : parseInt(product.idCombination);
  549. content += '<li name="customization"><div class="deleteCustomizableProduct" id="deleteCustomizableProduct_' + customizationId + '_' + productId + '_' + (productAttributeId ? productAttributeId : '0') + '"><a rel="nofollow" class="ajax_cart_block_remove_link" href="' + baseUri + '?controller=cart&amp;delete=1&amp;id_product=' + productId + '&amp;ipa=' + productAttributeId + '&amp;id_customization=' + customizationId + '&amp;token=' + static_token + '"></a></div><span class="quantity-formated"><span class="quantity">' + parseInt(this.quantity) + '</span>x</span>';
  550.  
  551. // Give to the customized product the first textfield value as name
  552. $(this.datas).each(function(){
  553. if (this['type'] == CUSTOMIZE_TEXTFIELD)
  554. {
  555. $(this.datas).each(function(){
  556. if (this['index'] == 0)
  557. {
  558. content += ' ' + this.truncatedValue.replace(/<br \/>/g, ' ');
  559. done = 1;
  560. return false;
  561. }
  562. })
  563. }
  564. });
  565.  
  566. // If the customized product did not have any textfield, it will have the customizationId as name
  567. if (!done)
  568. content += customizationIdMessage + customizationId;
  569. if (!hasAlreadyCustomizations) content += '</li>';
  570. // Field cleaning
  571. if (customizationId)
  572. {
  573. $('#uploadable_files li div.customizationUploadBrowse img').remove();
  574. $('#text_fields input').attr('value', '');
  575. }
  576. });
  577.  
  578. if (!hasAlreadyCustomizations)
  579. {
  580. content += '</ul>';
  581. if (!product.hasAttributes) content += '</dd>';
  582. }
  583. return (content);
  584. },
  585.  
  586.  
  587. //genarally update the display of the cart
  588. updateCart : function(jsonData) {
  589. //user errors display
  590. if (jsonData.hasError)
  591. {
  592. var errors = '';
  593. for (error in jsonData.errors)
  594. //IE6 bug fix
  595. if (error != 'indexOf')
  596. errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
  597. alert(errors);
  598. }
  599. else
  600. {
  601. ajaxCart.updateCartEverywhere(jsonData);
  602. ajaxCart.hideOldProducts(jsonData);
  603. ajaxCart.displayNewProducts(jsonData);
  604. ajaxCart.refreshVouchers(jsonData);
  605.  
  606. //update 'first' and 'last' item classes
  607. $('#cart_block .products dt').removeClass('first_item').removeClass('last_item').removeClass('item');
  608. $('#cart_block .products dt:first').addClass('first_item');
  609. $('#cart_block .products dt:not(:first,:last)').addClass('item');
  610. $('#cart_block .products dt:last').addClass('last_item');
  611.  
  612. //reset the onlick events in relation to the cart block (it allow to bind the onclick event to the new 'delete' buttons added)
  613. ajaxCart.overrideButtonsInThePage();
  614. }
  615. },
  616.  
  617. //update general cart informations everywhere in the page
  618. updateCartEverywhere : function(jsonData) {
  619. $('.ajax_cart_total').text($.trim(jsonData.productTotal));
  620.  
  621. if (parseFloat(jsonData.shippingCostFloat) > 0 || jsonData.nbTotalProducts < 1)
  622. $('.ajax_cart_shipping_cost').text(jsonData.shippingCost);
  623. else if (typeof(freeShippingTranslation) != 'undefined')
  624. $('.ajax_cart_shipping_cost').html(freeShippingTranslation);
  625. $('.ajax_cart_tax_cost').text(jsonData.taxCost);
  626. $('.cart_block_wrapping_cost').text(jsonData.wrappingCost);
  627. $('.ajax_block_cart_total').text(jsonData.total);
  628.  
  629. this.nb_total_products = jsonData.nbTotalProducts;
  630.  
  631. if (parseInt(jsonData.nbTotalProducts) > 0)
  632. {
  633. $('.ajax_cart_no_product').hide();
  634. $('.ajax_cart_quantity').text(jsonData.nbTotalProducts);
  635. $('.ajax_cart_quantity').fadeIn('slow');
  636. $('.ajax_cart_total').fadeIn('slow');
  637.  
  638. if (parseInt(jsonData.nbTotalProducts) > 1)
  639. {
  640. $('.ajax_cart_product_txt').each( function () {
  641. $(this).hide();
  642. });
  643.  
  644. $('.ajax_cart_product_txt_s').each( function () {
  645. $(this).show();
  646. });
  647. }
  648. else
  649. {
  650. $('.ajax_cart_product_txt').each( function () {
  651. $(this).show();
  652. });
  653.  
  654. $('.ajax_cart_product_txt_s').each( function () {
  655. $(this).hide();
  656. });
  657. }
  658. }
  659. else
  660. {
  661. $('.ajax_cart_quantity, .ajax_cart_product_txt_s, .ajax_cart_product_txt, .ajax_cart_total').each(function(){
  662. $(this).hide();
  663. });
  664. $('.ajax_cart_no_product').show('slow');
  665. }
  666. }
  667. };
  668.  
  669. function HoverWatcher(selector){
  670. this.hovering = false;
  671. var self = this;
  672.  
  673. this.isHoveringOver = function() {
  674. return self.hovering;
  675. }
  676.  
  677. $(selector).hover(function() {
  678. self.hovering = true;
  679. }, function() {
  680. self.hovering = false;
  681. })
  682. }
  683.  
  684. //when document is loaded...
  685. $(document).ready(function(){
  686. // expand/collapse management
  687. $('#block_cart_collapse').click(function(){
  688. ajaxCart.collapse();
  689. });
  690. $('#block_cart_expand').click(function(){
  691. ajaxCart.expand();
  692. });
  693. ajaxCart.overrideButtonsInThePage();
  694.  
  695. var cart_qty = 0;
  696. var current_timestamp = parseInt(new Date().getTime() / 1000);
  697.  
  698. if (typeof $('.ajax_cart_quantity').html() == 'undefined' || (typeof generated_date != 'undefined' && generated_date != null && (parseInt(generated_date) + 30) < current_timestamp))
  699. ajaxCart.refresh();
  700. else
  701. cart_qty = parseInt($('.ajax_cart_quantity').html());
  702.  
  703. /* roll over cart */
  704. var cart_block = new HoverWatcher('#cart_block');
  705. var shopping_cart = new HoverWatcher('#shopping_cart');
  706.  
  707. $("#shopping_cart a:first").hover(
  708. function() {
  709. $(this).css('border-radius', '3px 3px 0px 0px');
  710. if (ajaxCart.nb_total_products > 0 || cart_qty > 0)
  711. $("#cart_block").stop(true, true).slideDown(450);
  712. },
  713. function() {
  714. $('#shopping_cart a').css('border-radius', '3px');
  715. setTimeout(function() {
  716. if (!shopping_cart.isHoveringOver() && !cart_block.isHoveringOver())
  717. $("#cart_block").stop(true, true).slideUp(450);
  718. }, 200);
  719. }
  720. );
  721.  
  722. $("#cart_block").hover(
  723. function() {
  724. $('#shopping_cart a').css('border-radius', '3px 3px 0px 0px');
  725. },
  726. function() {
  727. $('#shopping_cart a').css('border-radius', '3px');
  728. setTimeout(function() {
  729. if (!shopping_cart.isHoveringOver())
  730. $("#cart_block").stop(true, true).slideUp(450);
  731. }, 200);
  732. }
  733. );
  734.  
  735. $('.delete_voucher').live('click', function() {
  736. $.ajax({
  737. type: 'POST',
  738. headers: { "cache-control": "no-cache" },
  739. async: true,
  740. cache: false,
  741. url:$(this).attr('href') + '?rand=' + new Date().getTime()
  742. });
  743. $(this).parent().parent().remove();
  744. if ($('body').attr('id') == 'order' || $('body').attr('id') == 'order-opc')
  745. {
  746. if (typeof(updateAddressSelection) != 'undefined')
  747. updateAddressSelection();
  748. else
  749. location.reload();
  750. }
  751. return false;
  752. });
  753.  
  754. $('#cart_navigation input').click(function(){
  755. $(this).attr('disabled', true).removeClass('exclusive').addClass('exclusive_disabled');
  756. $(this).closest("form").get(0).submit();
  757. });
  758. });
Advertisement
Add Comment
Please, Sign In to add comment