Looking for someone to take alook at this code and help

Joined
Mar 10, 2023
Messages
2
Reaction score
0
Heya I worked on this code to scan all guest orders on woocommerce and if it has a matching email to change the account to the matching account and if it doesnt find a matching account to create a new account and move the order to that new account. But it doesnt seem to be copying over the order to the new account. It does create the new account.

Any ideas why it isnt working?

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

function guest_order_scanner() {

global $wpdb;
$new_accounts = 0;
$merged_accounts = 0;

$guest_orders = $wpdb->get_results("
SELECT p.ID, pm.meta_value as email, pm2.meta_value as first_name, pm3.meta_value as last_name,
pm4.meta_value as billing_address, pm5.meta_value as billing_city, pm6.meta_value as billing_state,
pm7.meta_value as billing_phone
FROM {$wpdb->prefix}posts p
LEFT JOIN {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_billing_email'
LEFT JOIN {$wpdb->prefix}postmeta pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_billing_first_name'
LEFT JOIN {$wpdb->prefix}postmeta pm3 ON p.ID = pm3.post_id AND pm3.meta_key = '_billing_last_name'
LEFT JOIN {$wpdb->prefix}postmeta pm4 ON p.ID = pm4.post_id AND pm4.meta_key = '_billing_address_1'
LEFT JOIN {$wpdb->prefix}postmeta pm5 ON p.ID = pm5.post_id AND pm5.meta_key = '_billing_city'
LEFT JOIN {$wpdb->prefix}postmeta pm6 ON p.ID = pm6.post_id AND pm6.meta_key = '_billing_state'
LEFT JOIN {$wpdb->prefix}postmeta pm7 ON p.ID = pm7.post_id AND pm7.meta_key = '_billing_phone'
WHERE p.post_type = 'shop_order' AND p.post_status = 'wc-completed'
");

if ( count( $guest_orders ) > 0 ) {

foreach( $guest_orders as $guest_order ) {
$customer = get_user_by( 'email', $guest_order->email );
if ( ! $customer ) {
// Create a new user account if no matching account exists
$user_id = wp_create_user( $guest_order->email, wp_generate_password(), $guest_order->email );
if ( ! is_wp_error( $user_id ) ) {
// Update user meta with first and last name, billing address, city, state, and phone
update_user_meta( $user_id, 'first_name', $guest_order->first_name );
update_user_meta( $user_id, 'last_name', $guest_order->last_name );
update_user_meta( $user_id, 'billing_address_1', $guest_order->billing_address );
update_user_meta( $user_id, 'billing_city', $guest_order->billing_city );
update_user_meta( $user_id, 'billing_state', $guest_order->billing_state );
update_user_meta( $user_id, 'billing_phone', $guest_order->billing_phone );

// Assign the guest order to the new account
$wpdb->update(
$wpdb->prefix . 'woocommerce_order_items',
array(
'order_id' => $user_id,
'billing_address_1' => $guest_order->billing_address,
'billing_city' => $guest_order->billing_city,
'billing_state' => $guest_order->billing_state,
'billing_phone' => $guest_order->billing_phone,
),
array( 'order_id' => $guest_order->ID )
);

// Update all previous orders associated with the guest email to the new user ID
$previous_orders = get_posts(
array(
'post_type' => 'shop_order',
'meta_query' => array(
array(
'key' => '_billing_email',
'value' => $guest_order->email,
'compare' => '='
)
),
'post_status' => array( 'wc-completed' )
)
);
foreach ( $previous_orders as $previous_order ) {
wp_update_post(
array(
'ID' => $previous_order->ID,
'post_author' => $user_id
)
);
}

// Increment the new_accounts count if a new account was created
if ( ! empty( $user_id ) && ! is_wp_error( $user_id ) ) {
$new_accounts++;
echo '<tr><td>' . $guest_order->email . '</td><td>' . $guest_order->first_name . '</td><td>' . $guest_order->last_name . '</td></tr>';
}
}
}
// Assign the guest order to the registered account
else {
$wpdb->update(
$wpdb->prefix . 'woocommerce_order_items',
array(
'user_id' => $customer->ID,
'billing_address_1' => $guest_order->billing_address,
'billing_city' => $guest_order->billing_city,
'billing_state' => $guest_order->billing_state,
'billing_phone' => $guest_order->billing_phone,
),
array( 'order_id' => $guest_order->ID )
);
$merged_accounts++;

}
}
// Output summary of scanning results

echo '</tbody>';
echo '</table>';
}
}

// Define the function to handle the button click and initiate scanning
function initiate_guest_order_scanning() {
if ( isset( $_POST['guest_order_scanner_action'] ) && $_POST['guest_order_scanner_action'] === 'scan_guest_orders' ) {

// Verify nonce
if ( ! isset( $_POST['guest_order_scanner_nonce'] ) || ! wp_verify_nonce( $_POST['guest_order_scanner_nonce'], 'guest_order_scanner_nonce' ) ) {
wp_die( 'Security check failed' );
}

guest_order_scanner();
}
}

// Register the function to be called on the click of the scanning button
add_action( 'init', 'initiate_guest_order_scanning' );



// Add the menu page
function guest_order_scanner_menu() {
add_menu_page(
'Guest Order Scanner',
'Guest Order Scanner',
'manage_options',
'guest-order-scanner',
'guest_order_scanner_page',
'dashicons-search'
);
}
add_action( 'admin_menu', 'guest_order_scanner_menu' );

// Display the menu page
function guest_order_scanner_page() {

if ( isset( $_POST['guest_order_scanner_action'] ) && $_POST['guest_order_scanner_action'] === 'scan_guest_orders' ) {

// Verify nonce
if ( ! isset( $_POST['guest_order_scanner_nonce'] ) || ! wp_verify_nonce( $_POST['guest_order_scanner_nonce'], 'guest_order_scanner_nonce' ) ) {
wp_die( 'Security check failed' );
}


guest_order_scanner();

// Output summary of scanning results
printf( '<p>Scanning complete. %d new accounts created. %d orders merged with existing accounts.</p>', $new_accounts, $merged_accounts );
}

// Display the settings page
?>
<div class="wrap">
<h1><?php esc_html_e( 'Guest Order Scanner', 'guest-order-scanner' ); ?></h1>

<form method="post">
<?php wp_nonce_field( 'guest_order_scanner_nonce', 'guest_order_scanner_nonce' ); ?>
<input type="hidden" name="guest_order_scanner_action" value="scan_guest_orders" />
<p><?php esc_html_e( 'Click the button below to scan for guest orders that can be matched with existing customer accounts.', 'guest-order-scanner' ); ?></p>
<p><button class="button button-primary"><?php esc_html_e( 'Scan Guest Orders', 'guest-order-scanner' ); ?></button></p>
</form>
</div>
<?php
}

// Add plugin settings link
function guest_order_scanner_settings_link( $links ) {
$settings_link = '<a href="' . admin_url( 'admin.php?page=guest-order-scanner' ) . '">' . __( 'Settings', 'guest-order-scanner' ) . '</a>';
array_push( $links, $settings_link );
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'guest_order_scanner_settings_link' );
 
Joined
Jul 12, 2020
Messages
89
Reaction score
9
scan all guest orders on woocommerce and if it has a matching email to change the account to the matching account and if it doesnt find a matching account to create a new account and move the order to that new account.
If I'm understanding this correctly, your using too much code for such a simple process...break it down...
1. Place posted form info into placeholder variables.
2. [Scan] Check the posted email address against those that are currently listed.(no need for all that other stuff)
3. If email matches existing one append posted info.
4. If not create new entry.

Plus butchering HTML documents really isn't appealing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,560
Members
45,035
Latest member
HoTaKeDai

Latest Threads

Top