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
This commit is contained in:
Mike Jolley 2024-07-16 12:48:18 +01:00 committed by GitHub
parent b11ec75df2
commit b8fcc56e43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Hide account creation options not relevent to block checkout when using block checkout.

View File

@ -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
});
</script>