You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.0 KiB

5 years ago
$(document).ready( () => {
console.log('Load component - buy')
const component = $('.component-buy')
if(component.length) {
const details = component.find('.details')
if(details.length) {
const qty = details.find('.qty')
const plus = details.find('.plus')
const minus = details.find('.minus')
4 years ago
const addToCart = component.find('.add-to-cart')
const modal = component.find('.add-modal')
const modalClose = modal.find('.close')
qty.on('input', (e) => { $(e.currentTarget).val($(e.currentTarget).val().replace(/[^0-9]/g, '') || 1) })
plus.off('.click').on('click.click', () => {
const value = parseInt(qty.val())
qty.val(value + 1)
})
minus.off('.click').on('click.click', () => {
const value = parseInt(qty.val())
if(value > 1) {
qty.val(value - 1)
}
})
4 years ago
modalClose.off('.click').on('click.click', () => {
modal.fadeOut()
})
addToCart.off('.click').on('click.click', (e) => {
const button = $(e.currentTarget)
const pid = button.data('pid')
const name = button.data('name')
const price = button.data('price')
const selectedQty = parseInt(qty.val())
const modalImage = modal.find('.image')
const modalName = modal.find('.name')
const modalQty = modal.find('.qty')
const modalTotal = modal.find('.total')
modalImage.prop('src', `/images/products/${pid}.png`)
modalName.text(name)
modalQty.text(`Quantità: ${selectedQty}`)
modalTotal.text(`Costo totale: ${new Intl.NumberFormat('it-IT', { style: 'currency', currency: 'EUR'}).format( price * selectedQty)}`)
4 years ago
Apis.addToCart(pid, price, selectedQty).then( (data) => {
4 years ago
const headerCart = $('.component-header .cart-cta .counter')
if(headerCart.length) {
headerCart.text(data.cart.length)
}
4 years ago
modal.fadeIn()
}).catch( (error) => {
console.error(error)
})
})
}
}
})