Woocommerce:覆盖现有电子邮件所需的登录信息。将订单添加到用户帐户

标签 woocommerce checkout code-snippets

Woocommerce 设置:

  • 已禁用访客结帐。
  • 已启用结账时创建帐户(自动生成用户和密码)

如果用户已注册。但未登录。 结帐错误 “已使用您的电子邮件地址注册了一个帐户。请登录。”

我如何覆盖它 完成订单,并将订单与现有帐户绑定(bind)。 提示用户下次登录以便更快结帐

找不到任何可以做到这一点的代码片段或模块

还没有。很高兴得到奖励。

最佳答案

没有任何钩子(Hook)可以实现这一点。您可能需要修改 WC 核心中的 process_customer 函数 (plugins/woocommerce/includes/class-wc-checkout.php) Line#935 - 请记住,不鼓励编辑核心(更新后,您的更改将会丢失)

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'] : '';

                        if(email_exists($data['billing_email'])){
                        $creds = array();
                        $creds['user_login'] = $user_login;
                        $creds['user_password'] = $user_password;
                        $creds['remember'] = true;

                        $user = wp_signon($creds, false);

                        $customer_id = $user->ID;

                        wp_set_current_user($customer_id, $user_login);
                        wp_set_auth_cookie($customer_id, true, false);
                        do_action('wp_login', $user_login);
                        }else{
                        $customer_id = wc_create_new_customer( $data['billing_email'], $username, $password );
                        }

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

            wp_set_current_user( $customer_id );
            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();
        }

        // On multisite, ensure user exists on current site, if not add them before allowing login.
        if ( $customer_id && is_multisite() && is_user_logged_in() && ! is_user_member_of_blog() ) {
            add_user_to_blog( get_current_blog_id(), $customer_id, 'customer' );
        }

        // Add customer info from other fields.
        if ( $customer_id && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) {
            $customer = new WC_Customer( $customer_id );

            if ( ! empty( $data['billing_first_name'] ) ) {
                $customer->set_first_name( $data['billing_first_name'] );
            }

            if ( ! empty( $data['billing_last_name'] ) ) {
                $customer->set_last_name( $data['billing_last_name'] );
            }

            // If the display name is an email, update to the user's full name.
            if ( is_email( $customer->get_display_name() ) ) {
                $customer->set_display_name( $data['billing_first_name'] . ' ' . $data['billing_last_name'] );
            }

            foreach ( $data as $key => $value ) {
                // Use setters where available.
                if ( is_callable( array( $customer, "set_{$key}" ) ) ) {
                    $customer->{"set_{$key}"}( $value );

                    // Store custom fields prefixed with wither shipping_ or billing_.
                } elseif ( 0 === stripos( $key, 'billing_' ) || 0 === stripos( $key, 'shipping_' ) ) {
                    $customer->update_meta_data( $key, $value );
                }
            }

            /**
             * Action hook to adjust customer before save.
             *
             * @since 3.0.0
             */
            do_action( 'woocommerce_checkout_update_customer', $customer, $data );

            $customer->save();
        }

        do_action( 'woocommerce_checkout_update_user_meta', $customer_id, $data );
    }

如果您启用了允许客户在结帐时登录,则会出现从结帐页面登录的选项。

enter image description here

enter image description here

关于Woocommerce:覆盖现有电子邮件所需的登录信息。将订单添加到用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702729/

相关文章:

php - Woocommerce 退款电子邮件

css - Wordpress WooCommerce 更改 "Featured Products" slider 中元素的间距

php - 使用 ACF 的 Woocommerce 电子邮件通知主题的其他占位符

emacs - 与 AUCTeX 一起使用时,在 $0 后以 % 结尾的 yasnippets 行为很奇怪

c++ - 在 C++ 中获取总核心数量的跨平台代码片段是什么?

delphi - 有用的 Delphi 代码模板

使用自定义 Woocommerce 支付网关的 Javascript 覆盖 “Place Order button”

php - 在 WooCommerce 3 中重新排序结帐字段

php - 付款前在结账页面获取订单ID

php - 只允许在特定的 Woocommerce 结账字段上输入字母