允许WooCommerce现有客户无需登录即可结账

时间:2020-04-28 作者:Buzut

对于WooCommerce,如果电子邮件已与帐户关联,并且在结账页面上勾选了“创建帐户”复选框,则WooCommerce将要求客户在处理付款之前登录到其帐户。

这是有道理的,但它明显阻碍了转化率。我宁愿让客户完成交易,并透明地将订单与电子邮件的匹配帐户相关联。

然而,我不知道如何改变默认行为来实现这一点。

有什么想法吗?

多亏了@Gabriel的回答,if终于找到了解决办法。加布里埃尔指给我看process_customer 作用

原来之前有个钩子process_customer 被调用。这个钩子是woocommerce_checkout_process.

知道了这一点,这只是几句话:

检查用户是否尚未登录(&L);createaccount已选中如果是,请检查用户帐户是否与提供的电子邮件匹配如果不是,则不执行任何操作,默认行为良好如果是,则使用wp_set_current_user 将电子邮件匹配帐户设置为当前用户(不会使用户登录!)。这将产生将订单链接到现有账户(并更新发货/账单信息)的效果add_action(\'woocommerce_checkout_process\', function () { if (!is_user_logged_in() && $_POST[\'billing_email\'] && $_POST[\'createaccount\'] == 1) { $user = get_user_by(\'email\', $_POST[\'billing_email\']); if ($user) wp_set_current_user($user->ID); } });

1 个回复
最合适的回答,由SO网友:Gabriel Reguly 整理而成

您将允许用户填写电子邮件,使用它在数据库中查找用户id,然后将id用于订单。

一种可能的解决方案是更换function wc_create_new_customer( $email, $username = \'\', $password = \'\', $args = array() ) 具有自定义功能。

这是调用函数的地方,自定义函数需要做的就是找到正确的user\\u id。

    protected function process_customer( $data ) {
        $customer_id = apply_filters( \'woocommerce_checkout_customer_id\', get_current_user_id() );

        if ( ! is_user_logged_in() && ( $this->is_registration_required() || ! empty( $data[\'createaccount\'] ) ) ) {
            $username    = ! empty( $data[\'account_username\'] ) ? $data[\'account_username\'] : \'\';
            $password    = ! empty( $data[\'account_password\'] ) ? $data[\'account_password\'] : \'\';
            $customer_id = wc_create_new_customer(
                $data[\'billing_email\'],
                $username,
                $password,
                array(
                    \'first_name\' => ! empty( $data[\'billing_first_name\'] ) ? $data[\'billing_first_name\'] : \'\',
                    \'last_name\'  => ! empty( $data[\'billing_last_name\'] ) ? $data[\'billing_last_name\'] : \'\',
                )
            );

            if ( is_wp_error( $customer_id ) ) {
                throw new Exception( $customer_id->get_error_message() );
            }

            wc_set_customer_auth_cookie( $customer_id );

            // As we are now logged in, checkout will need to refresh to show logged in data.
            WC()->session->set( \'reload_checkout\', true );

            // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering.
            WC()->cart->calculate_totals();
        }

希望这能帮助你找到正确的方向。

干杯,加布里埃尔

相关推荐

使users.php搜索包含特定的用户元数据字段,而不会影响SQL查询本身

我想让管理员通过wordpress/wp admin/users中一个名为“indice de busqueda”的元值来搜索用户。php页面如何以这种方式连接到搜索框中,以便将查询更改为如下所示? $query = get_users([ \'meta_key\' => \'indice de busqueda\', \'meta_value\' => $search_value, \'meta_compare\'=>\