For the complete documentation index, see llms.txt. This page is also available as Markdown.

Examples

Simple examples to get you started with the Spiffy JavaScript API

Show/Hide Section Based on Field Value

This is a basic example of how to toggle the visibility of elements on your checkout based on the value of a field.

<script>

    // The ID of the section to toggle. Find it by inspecting the section element on
    // your checkout: the element ID is `section-<id>`, so this one is 91735.
    const sectionId = 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(() => {

        // Hide the section to start with. Nothing in this section is chargeable, so
        // there is no selection to clear alongside it.
        checkout.hide('section', sectionId);
        
        // 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) => {
            
            // 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) {
                checkout.show('section', sectionId);
            } else {
                checkout.hide('section', sectionId);
            }
        });
    });

</script>

Charge Flat-Rate Shipping by Country

This is a basic example of how to select/deselect checkout items based on the value of a field. This can be expanded to include all sorts of logic for determining items active on the checkout

Subscription Option Specific Discounts

This is an example of how to automatically swap between promo codes depending on which subscription option is selected

Hide/Show Payment Plans based on Field Value

This is a basic example of how to hide/show payment plan options based on the value of a field.

Last updated