-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update cart example #57
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,23 @@ $(function() { | |
}); | ||
|
||
var cart; | ||
client.createCart().then(function (newCart) { | ||
cart = newCart; | ||
}); | ||
var cartLineItemCount; | ||
if(localStorage.getItem('lastCartId')) { | ||
client.fetchCart(localStorage.getItem('lastCartId')).then(function(remoteCart) { | ||
cart = remoteCart; | ||
cartLineItemCount = cart.lineItems.length; | ||
renderCartItems(); | ||
}); | ||
} else { | ||
client.createCart().then(function (newCart) { | ||
cart = newCart; | ||
localStorage.setItem('lastCartId', cart.id); | ||
cartLineItemCount = 0; | ||
}); | ||
} | ||
|
||
|
||
|
||
var lineItemImages = {}; | ||
|
||
/* Fetch product and init | ||
============================================================ */ | ||
|
@@ -36,6 +48,12 @@ $(function() { | |
attachQuantityIncrementListeners(product); | ||
attachQuantityDecrementListeners(product); | ||
|
||
updateCartTabButton(); | ||
|
||
attachCheckoutButtonListeners(); | ||
|
||
closeCart(); | ||
|
||
}); | ||
|
||
/* Generate DOM elements for variant selectors | ||
|
@@ -99,42 +117,40 @@ $(function() { | |
function attachBuyButtonListeners(product) { | ||
$('.buy-button').on('click', function (event) { | ||
event.preventDefault(); | ||
var image = product.selectedVariantImage; | ||
var id = product.selectedVariant.id; | ||
lineItemImages[id] = image.src; | ||
addVariantToCart(product, product.selectedVariant, 1); | ||
addVariantToCart(product.selectedVariant, 1); | ||
}); | ||
} | ||
|
||
/* Increase product variant quantity in cart | ||
============================================================ */ | ||
function attachQuantityIncrementListeners(product) { | ||
$('.cart').on('click', '.quantity-increment', function() { | ||
var variantId = parseInt($(this).attr('data-variant-id')); | ||
var variantId = parseInt($(this).attr('data-variant-id'), 10); | ||
var variant = product.variants.filter(function (variant) { | ||
return (variant.id === variantId); | ||
})[0]; | ||
|
||
$(this).closest('.cart-item').addClass('js-working'); | ||
$(this).attr('disabled', 'disabled'); | ||
|
||
addVariantToCart(product, variant, 1); | ||
addVariantToCart(variant, 1); | ||
}); | ||
} | ||
|
||
/* Decrease product variant quantity in cart | ||
============================================================ */ | ||
function attachQuantityDecrementListeners(product) { | ||
$('.cart').on('click', '.quantity-decrement', function() { | ||
var variantId = parseInt($(this).attr('data-variant-id')); | ||
var variantId = parseInt($(this).attr('data-variant-id'), 10); | ||
var variant = product.variants.filter(function (variant) { | ||
return (variant.id === variantId); | ||
})[0]; | ||
|
||
$(this).closest('.cart-item').addClass('js-working'); | ||
$(this).attr('disabled', 'disabled'); | ||
|
||
addVariantToCart(product, variant, -1); | ||
addVariantToCart(variant, -1); | ||
}); | ||
} | ||
|
||
|
@@ -151,48 +167,59 @@ $(function() { | |
$('.cart').removeClass('js-active'); | ||
}); | ||
} | ||
closeCart(); | ||
|
||
|
||
/* Add 'quantity' amount of product 'variant' to cart | ||
============================================================ */ | ||
function addVariantToCart(product, variant, quantity) { | ||
function addVariantToCart(variant, quantity) { | ||
openCart(); | ||
|
||
cart.addVariants({ variant: variant, quantity: quantity }).then(function () { | ||
var $cartItemContainer = $('.cart-item-container'); | ||
var totalPrice = 0; | ||
|
||
$cartItemContainer.empty(); | ||
var lineItemEmptyTemplate = $('#cart-item-template').html(); | ||
var $cartLineItems = cart.lineItems.map(function (lineItem, index) { | ||
var $lineItemTemplate = $(lineItemEmptyTemplate); | ||
var itemImage = lineItem.image.src; | ||
$lineItemTemplate.find('.cart-item__img').css('background-image', 'url(' + itemImage + ')'); | ||
$lineItemTemplate.find('.cart-item__title').text(lineItem.title); | ||
$lineItemTemplate.find('.cart-item__variant-title').text(lineItem.variant_title); | ||
$lineItemTemplate.find('.cart-item__price').text(formatAsMoney(lineItem.line_price)); | ||
$lineItemTemplate.find('.cart-item__quantity').attr('value', lineItem.quantity); | ||
$lineItemTemplate.find('.quantity-decrement').attr('data-variant-id', lineItem.variant_id); | ||
$lineItemTemplate.find('.quantity-increment').attr('data-variant-id', lineItem.variant_id); | ||
|
||
if (index === cart.lineItems.length - 1) { | ||
$lineItemTemplate.addClass('js-hidden'); | ||
} | ||
return $lineItemTemplate; | ||
}); | ||
$cartItemContainer.append($cartLineItems); | ||
|
||
setTimeout(function () { | ||
$cartItemContainer.find('.js-hidden').removeClass('js-hidden'); | ||
}, 0) | ||
|
||
$('.cart .pricing').text(formatAsMoney(cart.subtotal)); | ||
|
||
cart.addVariants({ variant: variant, quantity: quantity }).then(function() { | ||
renderCartItems(); | ||
}).catch(function (errors) { | ||
console.log("Fail"); | ||
console.log(errors); | ||
console.log('Fail'); | ||
console.error(errors); | ||
}); | ||
|
||
updateCartTabButton(); | ||
} | ||
|
||
/* Render the line items currently in the cart */ | ||
function renderCartItems() { | ||
var $cartItemContainer = $('.cart-item-container'); | ||
var totalPrice = 0; | ||
|
||
$cartItemContainer.empty(); | ||
var lineItemEmptyTemplate = $('#cart-item-template').html(); | ||
var $cartLineItems = cart.lineItems.map(function (lineItem, index) { | ||
var $lineItemTemplate = $(lineItemEmptyTemplate); | ||
var itemImage = lineItem.image.src; | ||
$lineItemTemplate.find('.cart-item__img').css('background-image', 'url(' + itemImage + ')'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you avoiding using a templating system here for any reason? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just trying to keep the example as simple and small as possible. The focus of the example is meant to be the interactions with the SDK, so I didn't want to add in anything fancy with templating. I'm open to hearing other options though - whatever makes the example as simple and digestible as possible. @richgilbank thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just chatted with tyler in person - verdict was that it makes sense to not add an additional dependency for an example. |
||
$lineItemTemplate.find('.cart-item__title').text(lineItem.title); | ||
$lineItemTemplate.find('.cart-item__variant-title').text(lineItem.variant_title); | ||
$lineItemTemplate.find('.cart-item__price').text(formatAsMoney(lineItem.line_price)); | ||
$lineItemTemplate.find('.cart-item__quantity').attr('value', lineItem.quantity); | ||
$lineItemTemplate.find('.quantity-decrement').attr('data-variant-id', lineItem.variant_id); | ||
$lineItemTemplate.find('.quantity-increment').attr('data-variant-id', lineItem.variant_id); | ||
|
||
if (cartLineItemCount < cart.lineItems.length && (index === cart.lineItems.length - 1)) { | ||
$lineItemTemplate.addClass('js-hidden'); | ||
cartLineItemCount = cart.lineItems.length; | ||
} | ||
|
||
if (cartLineItemCount > cart.lineItems.length) { | ||
cartLineItemCount = cart.lineItems.length; | ||
} | ||
|
||
return $lineItemTemplate; | ||
}); | ||
$cartItemContainer.append($cartLineItems); | ||
|
||
setTimeout(function () { | ||
$cartItemContainer.find('.js-hidden').removeClass('js-hidden'); | ||
}, 0) | ||
|
||
$('.cart .pricing').text(formatAsMoney(cart.subtotal)); | ||
} | ||
|
||
/* Format amount as currency | ||
|
@@ -201,4 +228,32 @@ $(function() { | |
return '$' + parseFloat(amount, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString(); | ||
} | ||
|
||
|
||
/* Checkout listener | ||
============================================================ */ | ||
function attachCheckoutButtonListeners() { | ||
$('.btn--cart-checkout').on('click', function () { | ||
window.open(cart.checkoutUrl, '_self'); | ||
}); | ||
} | ||
|
||
/* Update cart tab button | ||
============================================================ */ | ||
function updateCartTabButton() { | ||
if (cart.lineItems.length > 0) { | ||
var totalItems = cart.lineItems.reduce(function(total, item) { | ||
return total + item.quantity; | ||
}, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made a note to move this work to the cart: #51 |
||
$('.btn--cart-tab .btn__counter').html(totalItems); | ||
$('.btn--cart-tab').addClass('js-active'); | ||
} else { | ||
$('.btn--cart-tab').removeClass('js-active'); | ||
$('.cart').removeClass('js-active'); | ||
} | ||
|
||
$('.btn--cart-tab').click(function() { | ||
openCart(); | ||
}); | ||
} | ||
|
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this live in its own file maybe? Keep large blobs out of the index?
I don't feel strongly either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's such a small example that I think we should keep it all in the same place. Keeps the example as straight forward as possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍