Pre-Select a Country in Restricted Shipping in SureCart

Tech Articles | December 4, 2025 | Blog, Coding, SureCart, Wordpress

This article is an update to one published last week on restricting the SureCart Shipping/ Billing Countries. You can view the original article here

This updated snippet makes it easier to set our wanted shipping/billing addresses, and in addition, it allows us to set one of them to be preselected at checkout.

Please Note: this is a WPCODEBOX snippet, so it has had its <script> tags removed

/* =====================================================
   CONFIGURATION
===================================================== */
const CONFIG = {
    enableShipping: true,                // Turn shipping restriction on/off
    enableBilling: false,                 // Turn billing restriction on/off
    enableLogging: false,                 // Console logs

    // Allowed countries (add as many as you want)
    allowedCountries: [
        { value: 'GB', label: 'United Kingdom' },
        // { value: 'US', label: 'United States' },
    ],

    // The country to automatically preselect
    defaultCountry: 'GB',                // Must match one of the countries above
};

/* =====================================================
   LOGGING HELPER
===================================================== */
function log(...args) {
    if (!CONFIG.enableLogging) return;
    console.log(...args);
}

/* =====================================================
   SHIPPING HANDLER
===================================================== */
function applyShippingUkOnly() {
    if (!CONFIG.enableShipping) {
        log('Shipping restriction disabled.');
        return true;
    }

    const shippingAddressElement =
        document.querySelector('sc-order-shipping-address')
        ?.shadowRoot
        ?.querySelector('sc-address, sc-compact-address');

    const shippingSelect =
        shippingAddressElement
        ?.shadowRoot
        ?.querySelector('sc-select');

    if (shippingSelect) {
        log('Shipping select found — applying country limits…');
        shippingSelect.choices = CONFIG.allowedCountries;
        shippingSelect.value = CONFIG.defaultCountry; // new preselected option
        return true;
    } else {
        log('Shipping select not found yet');
        return false;
    }
}

function pollShippingUntilFound() {
    let attempts = 0;
    const interval = 250;
    const maxAttempts = 40;

    const timer = setInterval(() => {
        attempts++;
        log(`Shipping attempt #${attempts}`);

        if (applyShippingUkOnly() || attempts >= maxAttempts) {
            clearInterval(timer);
            log(attempts >= maxAttempts
                ? 'Shipping: stopped after max attempts.'
                : 'Shipping: restriction applied and polling stopped.');
        }
    }, interval);
}

/* =====================================================
   BILLING HANDLER
===================================================== */
function pollBillingUntilFound() {
    if (!CONFIG.enableBilling) {
        log('Billing restriction disabled.');
        return;
    }

    let attempts = 0;
    const interval = 250;
    const maxAttempts = 40;

    const timer = setInterval(() => {
        attempts++;
        log(`Billing attempt #${attempts}`);

        const billingAddressElement =
            document.querySelector('sc-order-billing-address')
            ?.shadowRoot
            ?.querySelector('sc-address, sc-compact-address');

        const billingSelect =
            billingAddressElement
            ?.shadowRoot
            ?.querySelector('sc-select');

        if (billingSelect) {
            log('Billing select found — applying country limits…');
            billingSelect.choices = CONFIG.allowedCountries;
            billingSelect.value = CONFIG.defaultCountry; // Our New Pre-select
            clearInterval(timer);
            log('Billing: restriction applied and polling stopped.');
        } else if (attempts >= maxAttempts) {
            clearInterval(timer);
            log('Billing: stopped after max attempts.');
        } else {
            log('Billing select not found yet');
        }
    }, interval);
}

function setupBillingCheckboxListener() {
    if (!CONFIG.enableBilling) {
        log('Billing checkbox listener disabled.');
        return;
    }

    const billingCheckbox =
        document.querySelector('sc-order-billing-address')
        ?.shadowRoot
        ?.querySelector('sc-checkbox')
        ?.shadowRoot
        ?.querySelector('input');

    if (billingCheckbox && !billingCheckbox._listenerAdded) {
        log('Billing checkbox found — adding listener');
        billingCheckbox._listenerAdded = true;

        // If already unchecked (billing address visible), apply immediately
        if (!billingCheckbox.checked) {
            pollBillingUntilFound();
        }

        billingCheckbox.addEventListener('change', () => {
            log('Billing checkbox changed — rechecking billing fields…');
            pollBillingUntilFound();
        });

    } else if (!billingCheckbox) {
        log('Billing checkbox not found yet');
    }
}

/* =====================================================
   INITIALISATION
===================================================== */
function initCountryRestrictions() {
    log('initCountryRestrictions() starting…');

    // Shipping
    pollShippingUntilFound();

    // Billing: wait until the billing checkbox appears, then hook into it
    let attempts = 0;
    const maxAttempts = 20;
    const interval = 250;

    const timer = setInterval(() => {
        attempts++;
        log(`Billing checkbox setup attempt #${attempts}`);

        const billingCheckbox =
            document.querySelector('sc-order-billing-address')
            ?.shadowRoot
            ?.querySelector('sc-checkbox')
            ?.shadowRoot
            ?.querySelector('input');

        if (billingCheckbox || !CONFIG.enableBilling) {
            clearInterval(timer);
            setupBillingCheckboxListener();
        } else if (attempts >= maxAttempts) {
            clearInterval(timer);
            log('Billing checkbox not found — giving up.');
        }
    }, interval);
}

/* =====================================================
   BOOTSTRAP
===================================================== */
log('Registering SureCart event listeners…');

window.addEventListener('appload', () => {
    log('SureCart appload event fired.');
    initCountryRestrictions();
});

document.addEventListener('DOMContentLoaded', () => {
    log('DOM ready — running fallback init.');
    initCountryRestrictions();
});
JavaScript

Support the Author

Support my work
Really Useful Plugin Logo
Wpvideobank temp
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best