Created
March 11, 2025 05:21
-
-
Save mehrshaddarzi/b14aa7c05d6b150610701833fa3e2f3f to your computer and use it in GitHub Desktop.
auto create user after payment woocommerce order by mobile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_thankyou', 'ecommercehints_create_user_account_after_payment', 10, 1 ); | |
function ecommercehints_create_user_account_after_payment( $order_id ) { | |
// If user is logged in, do nothing because they already have an account | |
if( is_user_logged_in() ) return; | |
// Get the newly created order | |
$order = wc_get_order( $order_id ); | |
// Get the billing email address | |
$order_email = $order->get_billing_email(); | |
$order_phone = $order->get_billing_phone(); | |
// Check if there are any users with the billing email as user or email | |
// $email = email_exists( $order_email ); | |
$user = username_exists( $order_phone ); | |
// Get the order status (see if the customer has paid) | |
$order_status = $order->get_status(); | |
// Set to User If Not Login But Before Registered | |
if( $user != false and (int) $user >0 and (int) $order->get_customer_id() < 1 and ($order->has_status( 'processing' ) || $order->has_status( 'completed' )) ) { | |
$order->set_customer_id( $user ); | |
$order->save(); | |
} | |
// Check if the user exists and if the order status is processing or completed (paid) | |
if( $user === false and ($order->has_status( 'processing' ) || $order->has_status( 'completed' )) ) { | |
// Check on category ( multiple categories can be entered, separated by a comma ) | |
// Random password with 12 chars | |
$random_password = wp_generate_password(); | |
// Firstname | |
$first_name = $order->get_billing_first_name(); | |
// Lastname | |
$last_name = $order->get_billing_last_name(); | |
// Role | |
$role = 'customer'; | |
// Create new user with email as username, newly created password and user role | |
$user_id = wp_insert_user( | |
array( | |
'user_login' => $order_phone, | |
'user_email' => $order_email, | |
'user_pass' => $random_password, | |
'first_name' => $first_name, | |
'last_name' => $last_name, | |
'role' => $role, | |
) | |
); | |
// (Optional) WC guest customer identification | |
update_user_meta( $user_id, 'guest', 'yes' ); | |
// User's billing data | |
update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 ); | |
update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 ); | |
update_user_meta( $user_id, 'billing_city', $order->billing_city ); | |
update_user_meta( $user_id, 'billing_company', $order->billing_company ); | |
update_user_meta( $user_id, 'billing_country', $order->billing_country ); | |
update_user_meta( $user_id, 'billing_state', $order->billing_state ); | |
update_user_meta( $user_id, 'billing_email', $order->billing_email ); | |
update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name ); | |
update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name ); | |
update_user_meta( $user_id, 'billing_phone', $order->billing_phone ); | |
update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode ); | |
// User's shipping data | |
update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 ); | |
update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 ); | |
update_user_meta( $user_id, 'shipping_city', $order->shipping_city ); | |
update_user_meta( $user_id, 'shipping_company', $order->shipping_company ); | |
update_user_meta( $user_id, 'shipping_state', $order->shipping_state ); | |
update_user_meta( $user_id, 'shipping_country', $order->shipping_country ); | |
update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name ); | |
update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name ); | |
update_user_meta( $user_id, 'shipping_method', $order->shipping_method ); | |
update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode ); | |
// ns plugin | |
update_user_meta( $user_id, 'ns_user_mobile', $order_phone ); | |
update_user_meta( $user_id, 'ns_user_mobile_verified', 'yes' ); | |
// Digits Plugin | |
//$phoneNonZero = ltrim($order_phone, '0'); | |
//update_user_meta($user_id, 'digt_countrycode', '+98'); | |
//update_user_meta($user_id, 'digits_phone', '+98' . $phoneNonZero); | |
//update_user_meta($user_id, 'digits_phone_no', $phoneNonZero); | |
// Link past orders to this newly created customer | |
// wc_update_new_customer_past_orders( $user_id ); | |
// Update Past Orders | |
$customer_orders = wc_get_orders( | |
array( | |
'limit' => -1, | |
'billing_phone' => $order_phone, | |
'return' => 'ids', | |
) | |
); | |
if ( ! empty( $customer_orders ) ) { | |
foreach ( $customer_orders as $loop_order_id ) { | |
$loop_order = wc_get_order( $loop_order_id ); | |
if ( ! $loop_order ) { | |
continue; | |
} | |
$loop_order->set_customer_id( $user_id ); | |
$loop_order->save(); | |
} | |
} | |
} | |
} | |
/*add_action('init', function(){ | |
if(isset($_GET['outy'])) { | |
$customer_orders = wc_get_orders( | |
array( | |
'limit' => -1, | |
'billing_phone' => '099xxxxxx', | |
'return' => 'ids', | |
) | |
); | |
if ( ! empty( $customer_orders ) ) { | |
foreach ( $customer_orders as $order_id ) { | |
echo $order_id; | |
echo '<br />'; | |
} | |
} | |
exit; | |
} | |
});*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment