From b8fcc56e43fb903eb584b6638898de76ef5100b0 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 16 Jul 2024 12:48:18 +0100 Subject: [PATCH] Hide username checkbox if using block checkout (#49389) * Hide username checkbox if using block checkout * Hide correct field and fix render if first field is hidden * Changelog * Use array filter to prevent empty array item lingering --- ...-username-setting-for-block-checkout-49184 | 4 +++ .../settings/class-wc-settings-accounts.php | 26 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/update-hide-username-setting-for-block-checkout-49184 diff --git a/plugins/woocommerce/changelog/update-hide-username-setting-for-block-checkout-49184 b/plugins/woocommerce/changelog/update-hide-username-setting-for-block-checkout-49184 new file mode 100644 index 00000000000..da9e7f351e7 --- /dev/null +++ b/plugins/woocommerce/changelog/update-hide-username-setting-for-block-checkout-49184 @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Hide account creation options not relevent to block checkout when using block checkout. diff --git a/plugins/woocommerce/includes/admin/settings/class-wc-settings-accounts.php b/plugins/woocommerce/includes/admin/settings/class-wc-settings-accounts.php index b8c18059943..49cd3ade06e 100644 --- a/plugins/woocommerce/includes/admin/settings/class-wc-settings-accounts.php +++ b/plugins/woocommerce/includes/admin/settings/class-wc-settings-accounts.php @@ -11,6 +11,8 @@ if ( class_exists( 'WC_Settings_Accounts', false ) ) { return new WC_Settings_Accounts(); } +use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils; + /** * WC_Settings_Accounts. */ @@ -237,6 +239,25 @@ class WC_Settings_Accounts extends WC_Settings_Page { ), ); + // Change settings when using the block based checkout. + if ( CartCheckoutUtils::is_checkout_block_default() ) { + $account_settings = array_filter( + $account_settings, + function ( $setting ) { + return 'woocommerce_registration_generate_username' !== $setting['id']; + }, + ); + $account_settings = array_map( + function ( $setting ) { + if ( 'woocommerce_registration_generate_password' === $setting['id'] ) { + unset( $setting['checkboxgroup'] ); + } + return $setting; + }, + $account_settings + ); + } + /** * Filter account settings. * @@ -270,12 +291,15 @@ class WC_Settings_Accounts extends WC_Settings_Page { function updateInputs() { const isChecked = checkboxes.some(cb => cb && cb.checked); inputs.forEach(input => { + if ( ! input ) { + return; + } input.disabled = !isChecked; input.closest('td').classList.toggle("disabled", !isChecked); }); } - checkboxes.forEach(cb => cb.addEventListener('change', updateInputs)); + checkboxes.forEach(cb => cb && cb.addEventListener('change', updateInputs)); updateInputs(); // Initial state });