/** * This class manages the display, adding of items, and editing of the quote cart * @author Gabe Rose (gabe@equisolve.com) */ function QuoteCart() { var buttonSelector = ".js-add-quote"; this.cookieId = "QuoteCart"; this.items = Cookies.get(this.cookieId) || ''; this.$button = $(buttonSelector); this.quantityEl = document.getElementById('Quantity') || null; this.$button.on('click', function(e) { e.preventDefault(); var title = $(e.currentTarget).data('product'); //console.log('Title: ' + title); var quantity = this.quantityEl ? this.quantityEl.value : 1; this.addToQuoteCart(title, quantity); //this.displayModal($(e.target).data('product')); }.bind(this)); // On initalization, check to see if any items have a quantity of zero. If they do, remove them this.removeEmptyProducts(); } /** * When called, gets the product information via the Shopify JSON API, and then adds it to * the cart cookie */ QuoteCart.prototype.addToQuoteCart = function(title, quantity) { console.log('addToQuoteCart'); this.getProductInformation(title, null, function(result) { $.when(this.addToCookie({ id: result.id, quantity: quantity })).then(function(){ document.getElementById('quote-cart-frame').contentWindow.location.reload(); window.location.href = '/pages/quote-cart-checkout'; }); }.bind(this)); } /** * Looks up a product by title and returns all the necessary information for the quote cart * to function. * @param title | string Name of the product * @param id | mixed ID of the product * @param callback | function Callback function that works with the data returned from the AJAX call */ QuoteCart.prototype.getProductInformation = function(title, id, callback) { //console.log('getProductInformation'); //console.log(title); var id = id || null; var title = title || null; $.getJSON("/collections/quotable-items/products.json", function(data) { //console.log(title + ' ' + id); //console.log('Success: products.json accessed successfully'); }).error(function(){ //console.log('Error: There was an error reaching products.json'); }).fail(function(){ //console.log('Failed: There was an error reaching products.json'); }).done(function(data){ //console.log(title + ' ' + id); for (var i = 0; i < data.products.length; i++) { if ((title && data.products[i].title == title) || (id && data.products[i].id == id)) { console.log('ID: ' + data.products[i].id + ' Title: ' + data.products[i].title); return callback({ id: data.products[i].id, title: data.products[i].title, image: data.products[i].images[0].src, handle: data.products[i].handle }); } } //console.log('Success: products.json access is done'); }); } /** * Updates the quantity of a product for a quote * @param id | mixed The ID of the product * @param quantity | number The new quantity */ QuoteCart.prototype.updateProductQuantity = function(id, quantity) { var items = this.items.split(';'); for (var i = 0; i < items.length; i++) { var item = items[i], itemID = item.split(':')[0], itemQuantity = item.split(':')[1]; if (itemID == id) itemQuantity = quantity; item = itemID + ':' + itemQuantity; items[i] = item; } items = items.join(';'); this.updateCookie(items); this.removeEmptyProducts(); } /** * Checks for any products in the current quote with a quantity of zero, and removes them. */ QuoteCart.prototype.removeEmptyProducts = function() { var items = this.items.split(';'); for (var i = 0; i < items.length; i++) { var item = items[i], itemID = item.split(':')[0], itemQuantity = item.split(':')[1]; if (itemQuantity == 0) { items.splice(i,1); } } this.updateCookie(items.join(';')); } /** * Checks the current cookie and adds new data to the cookie * @param data | object An object with id and quantity properties */ QuoteCart.prototype.addToCookie = function(data) { //console.log('addToCookie'); var currentCookie = this.items; var newCookie = currentCookie; if (newCookie) newCookie += ";"; newCookie += data.id + ':' + data.quantity; Cookies.set(this.cookieId, newCookie); this.items = newCookie; //console.log('New cookie: ' + newCookie); } /** * Replaces the current stored cookie with an updated cookie. * @param cookie | string A cookie string */ QuoteCart.prototype.updateCookie = function(cookie) { Cookies.set(this.cookieId, cookie); this.items = cookie; } /** * Checks the current cookie for the supplied ID and removes it from the cookie if found * @param id | mixed The ID of the product to remove. */ QuoteCart.prototype.removeFromCookie = function(id) { var currentCookie = this.items; var newCookie = currentCookie.split(';'); for (var i = 0; i < newCookie.length; i++) { if (newCookie[i].indexOf(id)) { newCookie.splice(i,1); } } newCookie.join(';'); Cookies.set(this.cookieId, newCookie); this.items = newCookie; return; } /** * Checks the current cookie for duplicates and combines duplicated products into one */ QuoteCart.prototype.combineDuplicates = function() { if (this.items == '' || !this.items || this.items == null) { return; } var items = this.items.split(';'); var combinedItems = []; var allIDs = []; for (var i = 0; i < items.length; i++) { var item = items[i], itemID = item.split(':')[0], itemQuantity = item.split(':')[1]; allIDs.push(itemID); if (itemID in combinedItems) { combinedItems[itemID] = parseInt(combinedItems[itemID], 10) + parseInt(itemQuantity, 10); } else { combinedItems[itemID] = parseInt(itemQuantity, 10); } } var uniqueIDs = Array.from(new Set(allIDs)); var finalItems = []; uniqueIDs.forEach(function(item, i){ finalItems.push(item + ':' + combinedItems[item]); }); items = finalItems.join(';'); this.updateCookie(items); this.removeEmptyProducts(); return; } /** * Unsets cookie */ QuoteCart.prototype.unsetCookie = function() { // setting cookie to past date in order to unset Cookies.remove(this.cookieId); return; } /** * Displays a modal confirming that the user has added an item to their cart * @param title | string The title of the product added */ QuoteCart.prototype.displayModal = function(title) { //console.log('test'); $('body').append( '' ) } var cart = new QuoteCart();