使用购买站点A的数据在站点B上创建新帐户

时间:2021-08-11 作者:Matteo Feduzi

直到最近,我们还拥有一个单一的成员区域,该区域还集成了存储,数据都记录在一个数据库中。

现在,由于商品也即将上市,我们决定创建一个单独的商店,以保持一切清洁。

在该商店中,我们还销售对会员区的访问权限,我们希望执行以下操作:在站点A(商店)购买的用户也将在站点B(会员区)注册为新用户。

这是我迄今为止使用的代码,但我不知道如何继续:

//Code that links the DB of the site B
function connect_db_membership(){
global $db_membership;
$db_membership = new wpdb(\'USERNAME\', \'PASSWORD\', \'DATABASE_NAME\', \'HOSTNAME\');
}
add_action(\'init\', \'connect_db_membership\');

//Code that retrieves the first name, last name and email of the user who has just completed the purchase and passes it to the DB of site B.
add_action( \'woocommerce_order_status_completed\', \'change_role_on_purchase\' );
function change_role_on_purchase( $order_id ) {
    
    global $db_membership;
    
    $order = new WC_Order( $order_id );
    $items = $order->get_items();   
    
    $order = wc_get_order( $order_id );
    $order_data = $order->get_data();
    $order_billing_first_name = $order_data[\'billing\'][\'first_name\'];
    $order_billing_last_name = $order_data[\'billing\'][\'last_name\'];
    $order_billing_email = $order_data[\'billing\'][\'email\'];
    
}
注意:我想通过电子邮件到达的链接直接设置密码。

Useful information:

<这两个网站都在WordPress上

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

我设法通过PHP通过CURL请求解决问题,代码如下。

功能。站点A的php:

add_action( \'woocommerce_order_status_completed\', \'change_role_on_purchase\' );
function change_role_on_purchase( $order_id ) {
    
    $order = new WC_Order( $order_id );
    $items = $order->get_items();   
    
    foreach ( $items as $item_id => $item ) {
       $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
       if ( $product_id === 3960 ) { // Check product
            $order_data = $order->get_data();
            $order_billing_first_name = $order_data[\'billing\'][\'first_name\'];
            $order_billing_last_name = $order_data[\'billing\'][\'last_name\'];
            $order_billing_email = $order_data[\'billing\'][\'email\'];

            // API url
            $url = \'https://example.com/register-user\';

            // set post fields
            $data = array(
                \'name\' => $order_billing_first_name,
                \'lastname\' => $order_billing_last_name,
                \'email\'   => $order_billing_email,
            );

            $str = http_build_query($data);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            // execute!
            $response = curl_exec($ch);

            // close the connection, release resources used
            curl_close($ch);
        }
    }
}
注册用户页面上的代码。站点B中的php:

<?php
wp_head();
if(isset($_POST[\'name\'])){
    $name = $_POST[\'name\'];
    $lastname = $_POST[\'lastname\'];
    $username = strtolower($name . $lastname);
    $email = $_POST[\'email\'];
    //$user_id = username_exists( $username );
    if ( !email_exists( $email ) ) {
        $i = 1;
        while(username_exists($username)){
            $i++;
        }
        $username = $username . $i;
        $password = wp_hash_password(wp_generate_password());
        $user_id = wp_create_user( $username, $password, $email );
        $user = get_user_by( \'ID\', $user_id );
        // Remove role
        $user->remove_role( \'subscriber\' );
        // Add role
        $user->add_role( \'new_role\' );
    } else {
        $user = get_user_by( \'email\', $email );
        // Remove role
        $user->remove_role( \'subscriber\' );
        // Add role
        $user->add_role( \'new_role\' );
    }
}
wp_footer();
?>