Anton | настройка checkout
This commit is contained in:
48
index3.js
48
index3.js
@@ -27,6 +27,7 @@ const audienceContent = {
|
||||
const orderItemsField = document.getElementById('orderItemsField')
|
||||
const orderSuccess = document.getElementById('orderSuccess')
|
||||
const contactSuccess = document.getElementById('contactSuccess')
|
||||
const wooCommerceConfig = window.test1WooCommerce || null
|
||||
|
||||
let cart = []
|
||||
|
||||
@@ -132,6 +133,48 @@ const audienceContent = {
|
||||
|
||||
addToCartButtons.forEach(button => {
|
||||
button.addEventListener('click', () => {
|
||||
const productId = Number(button.dataset.productId || 0)
|
||||
|
||||
if (wooCommerceConfig && productId && wooCommerceConfig.addToCartUrl && wooCommerceConfig.checkoutUrl) {
|
||||
button.disabled = true
|
||||
|
||||
fetch(wooCommerceConfig.addToCartUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
product_id: String(productId),
|
||||
quantity: '1'
|
||||
})
|
||||
})
|
||||
.then(async response => {
|
||||
let payload = null
|
||||
|
||||
try {
|
||||
payload = await response.json()
|
||||
} catch (error) {
|
||||
payload = null
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('add_to_cart_failed')
|
||||
}
|
||||
|
||||
if (payload && payload.error && payload.product_url) {
|
||||
window.location.href = payload.product_url
|
||||
return
|
||||
}
|
||||
|
||||
window.location.href = wooCommerceConfig.checkoutUrl
|
||||
})
|
||||
.catch(() => {
|
||||
button.disabled = false
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
cart.push({
|
||||
name: button.dataset.name,
|
||||
volume: button.dataset.volume,
|
||||
@@ -149,6 +192,11 @@ const audienceContent = {
|
||||
})
|
||||
cartCloseButton.addEventListener('click', closeCart)
|
||||
checkoutButton.addEventListener('click', () => {
|
||||
if (wooCommerceConfig && wooCommerceConfig.checkoutUrl) {
|
||||
window.location.href = wooCommerceConfig.checkoutUrl
|
||||
return
|
||||
}
|
||||
|
||||
closeCart()
|
||||
openModal()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user