Home / eCommerce / Display Only Selected Countries on the Vendor Registration Form
Duplicate Snippet

Embed Snippet on Your Site

Display Only Selected Countries on the Vendor Registration Form

Streamline your registration form by showing only selected countries or remove others from the registration form using a simple customization.

Code Preview
php
<?php
add_action(
    'wp_enqueue_scripts',
    static function () {
        $registration_page_id = (int) get_option( 'wcv_signup_registration_page_id', 0 );
        if ( 0 === $registration_page_id || ! is_page( $registration_page_id ) ) {
            return;
        }
        $allowed_countries = array( 'AU', 'GB', 'US', 'CA' ); // ISO 3166-1 alpha-2 codes
        if ( empty( $allowed_countries ) ) {
            return;
        }
        wp_enqueue_script( 'wcv-signup' );
        wp_add_inline_script(
            'wcv-signup',
            sprintf(
                "window.addEventListener('load',function(){var allowed=%s,form=document.getElementById('wcv-registration-form');if(!form){return;}var select=form.querySelector('select[name=\"country\"]');if(!select){return;}Array.from(select.options).forEach(function(opt){if(opt.value && -1===allowed.indexOf(opt.value)){opt.remove();}});if(select.options.length&&allowed.indexOf(select.value)===-1){select.value=allowed[0];}select.dispatchEvent(new Event('change',{bubbles:true}));});",
                wp_json_encode( array_values( array_unique( $allowed_countries ) ) )
            ),
            'after'
        );
    },
    30
);

Comments

Add a Comment