Skip to content

Instantly share code, notes, and snippets.

@Buzut
Created November 5, 2020 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Buzut/9d9cb51c889c9edc6612dd999b5ddbc4 to your computer and use it in GitHub Desktop.
Save Buzut/9d9cb51c889c9edc6612dd999b5ddbc4 to your computer and use it in GitHub Desktop.
Link WooCommerce guest orders to existing customer account and create account if no user matches order billing email
<?php
// Make sure to allow a sufficient amount of ram to load all orders in memory
define('WP_MEMORY_LIMIT', '512M');
// Load WordPress core & plugins
require_once('wp-load.php');
// Get all completed orders
$query = new WC_Order_Query([
'type' => 'shop_order',
'status' => 'completed',
'limit' => -1
]);
$orders = $query->get_orders();
foreach($orders as $order) {
// If order is not linked to a customer account
if (empty($order->get_customer_id())) {
// First check if the customer has an email
// if not, proceed to next order (cannot create account w/o email)
if (empty($order->get_billing_email())) continue;
// Echo current order id to cli
echo 'Linking order to user account for order: ' . $order->get_id() . "\n";
// Check if account exist for order billing email
$user = get_user_by('email', $order->get_billing_email());
// If an account matches, just link order to exiting user account
if (!empty($user)) {
wc_update_new_customer_past_orders($user->ID);
continue;
}
/** Otherwise, create a new user account **/
// Generate a random password
$random_password = wp_generate_password();
// Create new user with email as username & random passwd
$user_id = wp_create_user($order->get_billing_email(), $random_password, $order->get_billing_email());
// Update account w/ order billing details
update_user_meta($user_id, 'billing_address_1', $order->get_billing_address_1());
update_user_meta($user_id, 'billing_address_2', $order->get_billing_address_2());
update_user_meta($user_id, 'billing_city', $order->get_billing_city());
update_user_meta($user_id, 'billing_company', $order->get_billing_company());
update_user_meta($user_id, 'billing_country', $order->get_billing_country());
update_user_meta($user_id, 'billing_email', $order->get_billing_email());
update_user_meta($user_id, 'billing_first_name', $order->get_billing_first_name());
update_user_meta($user_id, 'billing_last_name', $order->get_billing_last_name());
update_user_meta($user_id, 'billing_phone', $order->get_billing_phone());
update_user_meta($user_id, 'billing_postcode', $order->get_billing_postcode());
update_user_meta($user_id, 'billing_state', $order->get_billing_state());
// Update account w/ order shipping details
update_user_meta($user_id, 'shipping_address_1', $order->get_shipping_address_1());
update_user_meta($user_id, 'shipping_address_2', $order->get_shipping_address_2());
update_user_meta($user_id, 'shipping_city', $order->get_shipping_city());
update_user_meta($user_id, 'shipping_company', $order->get_shipping_company());
update_user_meta($user_id, 'shipping_country', $order->get_shipping_country());
update_user_meta($user_id, 'shipping_first_name', $order->get_shipping_first_name());
update_user_meta($user_id, 'shipping_last_name', $order->get_shipping_last_name());
update_user_meta($user_id, 'shipping_method', $order->get_shipping_method());
update_user_meta($user_id, 'shipping_postcode', $order->get_shipping_postcode());
update_user_meta($user_id, 'shipping_state', $order->get_shipping_state());
// Link order to newly created account
wc_update_new_customer_past_orders($user_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment