Last active
June 4, 2021 05:15
-
-
Save Kazuki-tam/66200b0c81ad7c29a8e5f71c86df648d to your computer and use it in GitHub Desktop.
Shopify動的チェックアウト対策
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
"use strict"; | |
const deliveryDateInput = document.querySelector('#delivery_date_input'); | |
const deliveryTimeInput = document.querySelector('#delivery_time'); | |
const observer = new MutationObserver((mutations) => { | |
mutations.forEach((mutation) => { | |
if (deliveryDateInput.value !== null) { | |
updateDeliveryDate(); | |
} | |
}); | |
}); | |
const config = { | |
attributes: true, | |
attributeFilter: ['value'] | |
}; | |
observer.observe(deliveryDateInput, config); | |
deliveryTimeInput.addEventListener('change', updateDeliveryDate); | |
function updateDeliveryDate() { | |
const deliveryDateInputValue = deliveryDateInput.value; | |
const deliveryTimeInputValue = deliveryTimeInput.value; | |
const data = { | |
attributes: { | |
'delivery_date': deliveryDateInputValue, | |
'delivery_time': deliveryTimeInputValue | |
} | |
}; | |
fetch('/cart/update.js', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(data), | |
}) | |
.then(response => response.json()) | |
.then(data => { | |
console.log('Success:', data); | |
}) | |
.catch((error) => { | |
console.error('Error:', error); | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment