0

Until recently we had a single members area that also integrated the store and the data was all recorded on a single DB.

Now, since the merchandising products are also coming out, we decided to create a separate store, to keep everything cleaner.

In this store we also sell access to the members area and we would like to do the following: The user who buys on site A (the store) is registered as a new user also on site B (the members area).

This is the code I've used so far, but I don't know how to continue:

//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'];
    
}

Note: I would like the password to be set directly through the link that arrives by email.

Useful information:

  • The two sites are both on WordPress;
  • The two sites are all hosted on the same hosting.
  • The sites are not on a multisite and I don't want to put them there since the only data they have to share is the user's data.
5
  • Is this a multisite, i.e. A and B share a users table? So you just need to assign a role on site B, and can use switch_to_blog() not a separate database connection?
    – Rup
    Commented Aug 11, 2021 at 15:16
  • If not I'd guess you can't use the built-in WordPress methods to do this unless you switch out the $wpdb global to your new connection, and I'm not sure I like that. I might also delay creating the $db_membership connection until you're sure you need it rather than doing it unconditionally in init()
    – Rup
    Commented Aug 11, 2021 at 15:18
  • Sites A and B are both on WordPress in the same hosting. However, the sites are not enabled to be multisite. I should therefore enable them for multisite, then what? Commented Aug 12, 2021 at 5:21
  • Does anyone have an idea on how I can do this? Thank you... Commented Aug 13, 2021 at 10:50
  • You could 1) write SQL to create the new users in the wp_users and usermeta tables over the remote database connection; 2) copy and paste the built-in WordPress user create functions and change the copies to use the remote database connection, then call those (beware of filters you might have running locally though); 3) use the REST API instead and make an authenticated REST call to site B with the user to create information (might need enhancing if you want to copy across the password hash too, I'm not sure if that's supported out of the box). Or probably more options too.
    – Rup
    Commented Aug 13, 2021 at 10:58

1 Answer 1

0

I managed to solve via CURL requests via PHP, the code is as follows.

functions.php of site A:

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);
        }
    }
}

Code on page-register-user.php in the site B:

<?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();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.