Public Ticket #2736603

How to add different registration fields in different languages (B2BKing & WPML)

Closed
Comments
6 Tickets 2
Erkko Mäkinen started the conversation 6 years ago on March 30, 2021 at 4:03pm

Hi,

Is it possible to create different registration fields for different languages? I'm using WPML.

If i disable registration field on another language it still appears in all languages. I know that is the way to translate registration fields according to the documentation but what about showing specific field only in one language?

2,762
KingsPlugins replied 6 years ago on March 31, 2021 at 5:27pm

Hi Erkko,

I've thought about this and looked into the code - it's not possible right now with the way our plugin works. We would need to extend the functionality so that the plugin can understand what language is currently selected with WPML. We can look at an improvement here for future updates, but right now I don't see a way to do it.


That being said, I'm pretty handy with JavaScript. If you give me a link to your registration page and tell me which fields need to be hidden for which language, I can send you a JS snippet to add to your site to handle it for that specific page.


Kind regards,

Stefan


6 Tickets 2
Erkko Mäkinen replied 6 years ago on April 6, 2021 at 10:57am

For now I only have the site only on my local development environment. Can you provide a little example for example as a pseudo code so I could try to do it myself?

So my goal is to show one specific field for one specific language. All the other fields are same in all languages. That field should also be required for that language so just hiding it for other languages wouldn't work. I could mark it as required with javascript (and not in the backend) but is there some better way to achieve the funcionality?

2,762
KingsPlugins replied 5 years ago on April 8, 2021 at 12:59pm

Ideally, only enabled fields would show in their own specific languages. But unfortunately that requires some major overhauls to our code in many places. So at the moment, I think setting it up with JS is the easiest way to do it.


I have written some code below that hopefully can help and makes sense. Required can be added / removed with JavaScript, so we can fully control it that way as well. 

jQuery(document).ready(function(){
    // run function when page loads
    setTimeout(function(){
        showHideRegistrationFieldsCustom();
    }, 250);
    // run function everytime fields are shown/hidden
    jQuery('#b2bking_registration_roles_dropdown').change(showHideRegistrationFieldsCustom);
    jQuery('.b2bking_country_field_selector select').change(showHideRegistrationFieldsCustom);
    jQuery('select#billing_country').change(showHideRegistrationFieldsCustom);
    function showHideRegistrationFieldsCustom(){
        // set timeout so that function runs after regular plugin js functions
        setTimeout(function(){
            // get current WPML language
            var selectedLang = b2bking_display_settings.purchase_lists_language_option;
            // get field and container
            var fieldInput = jQuery('input[name=b2bking_custom_field_7381]');
            var fieldContainer = jQuery('input[name=b2bking_custom_field_7381]').parent().parent();
            if (selectedLang === 'English'){
                // show english specific field and make it required
                jQuery(fieldContainer).css('display','block');
                jQuery(fieldInput).prop('required','true');
            } else {
                // hide english specific field and remove required
                jQuery(fieldContainer).css('display','none');
                jQuery(fieldInput).removeAttr('required');
            }
        }, 250);
    }
});

In the above code, I think the only thing that needs to be changed is the field name 

b2bking_custom_field_7381

and the language


However I haven't had a chance to test it with WPML.


Kind regards,

Stefan

6 Tickets 2
Erkko Mäkinen replied 5 years ago on May 3, 2021 at 3:02pm

Thank you! This solution worked for me.

Would it be also possible to select default country for specific language? I use the Country + State field and it would be great if it the country is selected automatically by chosen language.

1
2,762
KingsPlugins replied 5 years ago on May 3, 2021 at 7:21pm

Glad that worked!


Yes, it can be done.

Here is the code:

jQuery(document).ready(function(){
    if(b2bking_display_settings.purchase_lists_language_option === 'English'){
        jQuery('.b2bking_country_or_state').parent().find('select').select2('destroy');
        jQuery('.b2bking_country_or_state').parent().find('select').val('GB');
        jQuery('.b2bking_country_or_state').parent().find('select').select2();
    }
    if(b2bking_display_settings.purchase_lists_language_option === 'Finnish'){
        jQuery('.b2bking_country_or_state').parent().find('select').select2('destroy');
        jQuery('.b2bking_country_or_state').parent().find('select').val('FI');
        jQuery('.b2bking_country_or_state').parent().find('select').select2();
    }
}

Kind regards,

Stefan

6 Tickets 2
Erkko Mäkinen replied 5 years ago on May 3, 2021 at 7:49pm

This is almost what I need. Only problem is that the state field is not hiding like it normally would if I clicked Finland manually from the select field. Can it also be hidden in a way that it is still possible to pick some other country that has the state field?

2,762
KingsPlugins replied 5 years ago on May 6, 2021 at 8:21pm

I think the following should do it:

You can see I added a line of code to the Finnish bracket to hide the state.

jQuery(document).ready(function(){
    if(b2bking_display_settings.purchase_lists_language_option === 'English'){
        jQuery('.b2bking_country_or_state').parent().find('select').select2('destroy');
        jQuery('.b2bking_country_or_state').parent().find('select').val('GB');
        jQuery('.b2bking_country_or_state').parent().find('select').select2();
    }
    if(b2bking_display_settings.purchase_lists_language_option === 'Finnish'){
        jQuery('.b2bking_country_or_state').parent().find('select').select2('destroy');
        jQuery('.b2bking_country_or_state').parent().find('select').val('FI');
        jQuery('.b2bking_country_or_state').parent().find('select').select2();
        jQuery('#billing_state_field').css('display','none');
    }
}



Sign In