Anton | настройка checkout

This commit is contained in:
ab.an.ev@yandex.ru
2026-04-02 11:44:35 +03:00
parent 50d6fe60ed
commit 3be8f9d1c4
5 changed files with 390 additions and 6 deletions

View File

@@ -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()
})