You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅在使用WooCommerce Payments Express Checkout时自动生成账号密码

实现方案

所需核心钩子

你需要使用的filter是 woocommerce_registration_generate_password,该钩子用于控制WooCommerce注册新账号时是否自动生成密码,返回布尔值true则自动生成,返回false则允许用户自定义密码。

配套逻辑说明

你需要额外添加判断逻辑识别当前结账流程是否为Express Checkout快速结账场景:WooCommerce Payments触发Express Checkout时,会在用户会话中写入专属标识wcpay_express_checkout_customer_initiated,同时请求参数中也会携带快速结账相关标记,可通过这两个维度做场景判定。

代码示例

将以下代码添加到你的主题functions.php文件或自定义功能插件中即可生效:

add_filter( 'woocommerce_registration_generate_password', 'custom_express_checkout_auto_gen_pwd', 10, 1 );
function custom_express_checkout_auto_gen_pwd( $default_generate ) {
    $is_express_checkout = false;

    // 校验会话中的快速结账触发标识
    if ( class_exists( 'WC_Session' ) && WC()->session ) {
        if ( WC()->session->get( 'wcpay_express_checkout_customer_initiated' ) ) {
            $is_express_checkout = true;
        }
    }

    // 校验请求参数中的快速结账标记,双重保障
    if ( isset( $_REQUEST['wcpay_express_checkout'] ) || isset( $_REQUEST['express_checkout'] ) ) {
        $is_express_checkout = true;
    }

    // 仅快速结账场景自动生成密码,其余场景关闭自动生成
    return $is_express_checkout ? true : false;
}

配置注意事项

请先关闭WooCommerce后台路径「WooCommerce > Settings > Accounts & Privacy」下的全局「创建账号时自动生成账号密码」选项,上述代码会在快速结账场景下强制覆盖该配置开启自动生成,其余场景保留用户手动设置密码的权限,完全匹配你的需求。

内容的提问来源于stack exchange,提问作者l_r

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 15:21:01