Examples
Simple examples to get you started with the Spiffy JavaScript API
Show/Hide Section Based on Field Value
<script>
// Declare a constant `sectionToToggle` which contains a CSS selector for the section that will be toggled.
// This points to the HTML element with the ID 'section-91735'.
const sectionToToggle = '#section-91735';
// Declare a constant `customFieldName` which specifies the name of the custom field we're interested in monitoring.
// This is named "24_occupancy_options" in this case.
const customFieldName = "24_occupancy_options";
// Declare a constant `fieldValueToShow` that holds the value the custom field must have for the section to be visible.
// In this instance, the section will be shown when the field's value is "double-occupancy".
const fieldValueToShow = "double-occupancy";
// The `checkout.ready()` function ensures the code inside runs once the Spiffy checkout system is fully initialized.
checkout.ready(() => {
// Add an event listener to monitor any changes in checkout fields.
// The callback function runs whenever a checkout field value changes.
checkout.on('change:field', (fields) => {
// Initially hide the specified section by setting its CSS 'display' property to 'none'.
document.querySelector(sectionToToggle).style.display = 'none';
// Filter the `fields` array to find the custom field of interest by its name.
// The filter function returns an array, but we're only interested in the first item ([0]),
// as field names should be unique.
let field = fields.filter(f => f.name == customFieldName)[0];
// Check if the field's value matches `fieldValueToShow`.
if (field && field.value == fieldValueToShow) {
// If the value matches, make the specified section visible again by resetting its 'display' property.
document.querySelector(sectionToToggle).style.display = null;
}
});
});
</script>
Charge Flat-Rate Shipping by Country
Subscription Option Specific Discounts
Hide/Show Payment Plans based on Field Value
Last updated