注册后如何将 WooCommerce 访客订单链接到客户帐户

问题描述 投票:0回答:2

我们的场景是:

访客用户进入网站并下一个或多个订单,无需注册。一段时间后,他决定在该网站上注册。

现在注册后如何将客人订单链接到客户帐户?

我使用以下代码,但此代码仅适用于已经注册但购买时未登录的用户。有什么建议吗?

//assign user in guest order
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 1 );
function action_woocommerce_new_order( $order_id ) {
    $order = new WC_Order($order_id);
    $user = $order->get_user();
    
    if( !$user ){
        //guest order
        $userdata = get_user_by( 'email', $order->get_billing_email() );
        if(isset( $userdata->ID )){
            //registered
            update_post_meta($order_id, '_customer_user', $userdata->ID );
        }else{
            //Guest
        }
    }
}
wordpress woocommerce hook-woocommerce orders
2个回答
4
投票

您可以使用

woocommerce_created_customer
操作挂钩和 wc_update_new_customer_past_orders() 函数

所以你得到:

function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
    // Link past orders to this newly created customer
    wc_update_new_customer_past_orders( $customer_id );
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 ); 

0
投票

我正在使用下面的代码,它似乎有效。如果用户创建帐户,则会显示他们之前使用该帐户下的订单。

有没有办法提取他们的名字、姓氏、账单和送货信息并将其显示在他们的帐户上?目前,它只提取他们用来创建订单的电子邮件。

这是我使用的代码

    <?php

/* Credits: https://stackoverflow.com/questions/71112271/how-to-link-woocommerce-guest-orders-to-customer-account-after-registration */

function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
    // Link past orders to this newly created customer
    wc_update_new_customer_past_orders( $customer_id );
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 ); 
© www.soinside.com 2019 - 2024. All rights reserved.