我正在尝试为 Woocommerce 开发一个插件,以提供更好的用户注册流程。它需要始终禁用“创建帐户时,向新用户发送一个链接以设置其密码”字段。
我不知道在哪里连接到 WooCommerce 中设置管理字段。我似乎一直盯着这些文档,但我无法理解它。我按照面包屑导航到了此页面Hooks Reference。
woocommerce_admin_field_
,但我无法使用add_filter()
,例如:
add_filter('woocommerce_admin_field_woocommerce_registration_generate_password', [Settings::class, 'setRequiredWoocommerceSettings']);
我尝试这样做:
public static function setRequiredWoocommerceSettings($setting) {
$isSendPasswordLinkActive = get_option('woocommerce_registration_generate_password');
if ($isSendPasswordLinkActive == 'yes') {
Utils::debug($settings);
return 'no';
}
}
我可以使用以下方式获得该选项:
get_option('woocommerce_registration_generate_password');
但我不太确定需要做什么来确保该字段始终被选中。
如有任何帮助,我们将不胜感激。
要使用的正确复合挂钩是
woocommerce_get_settings_{$tab_section}
,其中“帐户和隐私”部分的 $tab_section
值为 account
。
要禁用选项
woocommerce_registration_generate_username
并正确删除相关复选框字段(设置),请从 WooCommerce 常规设置“帐户和隐私”部分选项卡内容中使用以下命令:
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;
}
代码位于活动子主题的functions.php 文件中(或插件中)。已测试并有效。
然后该设置将被删除,并且该选项设置为“否”: