SureCart – Restrict Shipping / Billing Address to UK (Or any Country)

Tech Articles | November 29, 2025 | Bubble, Coding, SureCart, Wordpress

In this short article, we will cover how I did this and the options I put in place for this article I am using WPCodeBox and have it as a JS script. This means the <script> </script> tags have been stripped out. You could also run it as part of a PHP WP_footer setup if you know how to do this.

Snippet Settings

surecart shipping billing restriction

I originally wrote this snippet to go with my Royal Mail bridge for SureCart to allow me to automatically pass details of sales to Royal Mail Click and Drop. You can see this video below and see how this works and what it entails.

If you want to see this snippet in action and what it does, please watch the short video below, which shows it in action. In my example, to go with the bridge, I only need shipping details, and I only want shipping within the UK.

The Code Snippet

Please remember this is JavaScript, it doesn’t have any <script> </script> tags as I don’t need them in WPCodeBox

    // ============================
    // CONFIG
    // ============================
    const CONFIG = {
        enableShipping: true,   // true = restrict shipping to UK
        enableBilling: false,    // true = restrict billing to UK
        enableLogging: false,    // true = show console logs
    };

    function log(...args) {
        if (!CONFIG.enableLogging) return;
        console.log(...args);
    }

    log('SureCart country restriction snippet loaded');

    // ============================
    // SHIPPING
    // ============================
    function applyShippingUkOnly() {
        if (!CONFIG.enableShipping) {
            log('Shipping restriction disabled (enableShipping = false)');
            return true; // treat as "done"
        }

        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 — setting to UK only');
            shippingSelect.choices = [                
                { value: 'GB', label: 'United Kingdom' }
            ];
            return true;
        } else {
            log('Shipping select not found (yet)');
            return false;
        }
    }

    function pollShippingUntilFound() {
        let attempts = 0;
        const maxAttempts = 40;   // 40 * 250ms = 10s
        const interval = 250;

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

            if (applyShippingUkOnly() || attempts >= maxAttempts) {
                if (attempts >= maxAttempts) {
                    log('Stopping shipping polling (max attempts reached).');
                } else {
                    log('Shipping restriction applied, stopping shipping polling.');
                }
                clearInterval(timer);
            }
        }, interval);
    }

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

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

        const timer = setInterval(() => {
            attempts++;
            log(`Billing check 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 — setting to UK only');
                billingSelect.choices = [
                    { value: 'GB', label: 'United Kingdom' }
                ];
                log('Billing restriction applied, stopping billing polling.');
                clearInterval(timer);
            } else if (attempts >= maxAttempts) {
                log('Stopping billing polling (max attempts reached).');
                clearInterval(timer);
            } else {
                log('Billing select not found (yet)');
            }
        }, interval);
    }

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

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

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

            // If billing fields are already showing (checkbox unchecked), start polling immediately
            if (!billingCheckbox.checked) {
                log('Billing checkbox initially unchecked — starting billing polling now');
                pollBillingUntilFound();
            }

            billingCheckbox.addEventListener('change', () => {
                log('Billing checkbox changed — starting billing polling');
                pollBillingUntilFound();
            });
        } else if (!billingCheckbox) {
            log('Billing checkbox not found (yet)');
        }
    }

    function initUkRestrictions() {
        log('initUkRestrictions() running…');

        // Start shipping polling (if enabled)
        pollShippingUntilFound();

        // Try to set up the billing checkbox; if it’s not there yet, poll for it briefly
        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) {
                log('Stopping billing checkbox setup polling (max attempts reached).');
                clearInterval(timer);
            } else {
                log('Billing checkbox still not found');
            }
        }, interval);
    }

    log('Registering SureCart appload listener…');

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

    document.addEventListener('DOMContentLoaded', () => {
        log('DOMContentLoaded — starting UK restriction setup…');
        initUkRestrictions();
    });
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