Search code examples
phpwordpresswoocommercesettingsadmin

How to alter some Woocommerce admin general settings


I am trying to develop a plugin for Woocommerce that provides a better user signup flow. It would require the "When creating an account, send the new user a link to set their password" field to be disabled at all times.

I can't figure out where to hook into to set admin fields in WooCommerce. I have been staring at the docs forever, it seems like, and I can't make sense of it. I sort of followed the breadcrumbs to this page Hooks Reference.

Where I found the following hook here woocommerce_admin_field_, but I am not able to use add_filter() with it, like:

add_filter('woocommerce_admin_field_woocommerce_registration_generate_password', [Settings::class, 'setRequiredWoocommerceSettings']);

And I tried to do:

public static function setRequiredWoocommerceSettings($setting) {
    $isSendPasswordLinkActive = get_option('woocommerce_registration_generate_password');
    if ($isSendPasswordLinkActive == 'yes') {
        Utils::debug($settings);
        return 'no';
    }
}

I'm able to get the option using:

get_option('woocommerce_registration_generate_password');

But I am not really sure what I need to do to make sure this field is always checked off.

Any help would be appreciated.


Solution

  • The right composite hook to be used is woocommerce_get_settings_{$tab_section}, where $tab_section value for "Accounts and Privacy" section is account.

    To disable the option woocommerce_registration_generate_username and remove properly the related checkbox field (setting), from WooCommerce general settings "Accounts and Privacy" section tab content, use the following:

    add_filter( 'woocommerce_get_settings_account', 'woocommerce_general_settings_account_tab_content', 10, 2 );
    function woocommerce_general_settings_account_tab_content( $settings, $current_section ) {
        // The current section is an empty string as there are no additional settings sub-tab for this section
        if( $current_section === '' ) {
            // The setting field ID to target
            $option_slug = 'woocommerce_registration_generate_username'; 
            // Loop through WooCommerce general "account" settings
            foreach ( $settings as $index => $field ) {
                // Target the required specific setting
                if( $field['id'] === $option_slug ) {
                    // If the option is activayed
                    if ( get_option($option_slug) ==='yes' ) {
                        update_option($option_slug, 'no'); // Disable it
                    }
                    // Remove the field from settings
                    unset($settings[$index]);
                    break; // stop the loop
                }
            }
        }
        return $settings;
    }
    

    Code goes in functions.php file of the active child theme (or in a plugin). Tested and works.

    Then the setting will be removed, and the option set to "no":

    enter image description here