Reputation: 93
I'm working on an e-commerce project. When a customer purchases a product, they are assigned a Customer Code based on their first name, last name and sequential numbers. It's used by the accounting software.
With help from StackOverflow I've both corrected my code and added additional code that creates a Guest User and custom-generated code. The problem is that the code isn't linked to an order, eg. when a new customer buys a product (such as with the order number WC0001979) there isn't anything to show the Customer Code with order number (and vice versa).
I had a stab at the code. This is it.
add_action( 'woocommerce_checkout_update_order_meta', 'include_customer_code_on_order', 10, 2 );
function include_customer_code_on_order( $order_id, $posted_data ) {
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
if ( $user_id ) {
$customer_code = get_user_meta( $user_id, 'ipx_customer_code', true );
if ( $customer_code ) {
$order->update_meta_data( 'ipx_customer_code', $customer_code );
$order->save();
}
}
}
I also attempted to add it to the order screen in Woocommerce. The label was successful - I could see "IPX Customer Code" underneath the email and telephone number. The problem is that it display the actual code itself.
Any assistance would be appreciated.
After some help with this, I realised that the new code (from Loic) only linked Guest users to orders. It should be every role. I amended the code and it works for both registered and unregistered (Guest) users.
add_action( 'woocommerce_order_status_changed', 'link_ipx_customer_code_to_the_order', 10, 4 );
function link_guest_ipx_customer_code_to_the_order( $order_id, $status_from, $status_to, $order ) {
// Check that no "IPX Customer Code is assigned
if ( ! $order->get_meta('ipx_customer_code') ) {
// Get the WP_User object from the billing email
$user = get_user_by( 'email', $order->get_billing_email() );
// Check that user exist and has a "IPX Customer code"
if ( $user && isset($user->ipx_customer_code) ) {
$order->update_meta_data( 'ipx_customer_code', esc_attr($user->ipx_customer_code) ); // Set the IPX code
$order->save(); // save new meta data
}
}
}
I went a stage further at that point and integrated the Customer Code into the order screen using the following code:
// Display IPX Customer Code on Order screen
add_action( 'woocommerce_admin_order_data_after_billing_address', 'ipx_collection_weight_display_admin_order_meta' );
function ipx_collection_weight_display_admin_order_meta( $order ) {
echo '<p><strong>IPX Customer Code:</strong> ' . get_post_meta( $order->get_id(), 'ipx_customer_code', true ) . '</p>';
}
I've also started to add the Customer Code into emails sent to the admin but it's a work in progress.
add_action( 'woocommerce_email_order_meta', 'add_ipx_customer_code_to_email', 10, 3 );
function add_ipx_customer_code_to_email( $order, $sent_to_admin, $plain_text ){
$ipx_customer_code = get_post_meta( $order->get_order_number(), 'ipx_customer_code', true );
// Don't display anything if it is empty
if( empty( $ipx_customer_code ) )
return;
if ( $plain_text === false ) {
echo '<ul>
<li><strong>IPX Customer Code: </strong>' . $ipx_customer_code . '</li>
</ul>';
} else {
echo "IPX Customer Code: ". $ipx_customer_code;
}
}
Upvotes: 1
Views: 112
Reputation: 254271
The context: In my previous answer, I use a custom function hooked in woocommerce_checkout_order_processed
action hook with a priority of 10
, that registers the guest user using the WordPress wp_insert_user()
function, which trigger your function that generate (and save as user metadata) a unique "IPX customer code".
So what we can try is to use a custom function hooked in the same action hook, with a greater priority, to get that registered "IPX customer code" from user metadata.
This way, we are sure that the guest has been registered and has a unique "IPX customer code" assigned.
add_action( 'woocommerce_checkout_order_processed', 'link_guest_ipx_code_to_the_order', 20, 3 );
function link_guest_ipx_code_to_the_order( $order_id, $posted_data, $order ) {
// Get the WP_User object from the billing email
$user = get_user_by( 'email', $order->get_billing_email() );
// Check that user exist and has an "ipx customer code" and has a guest user role
if ( $user && isset($user->ipx_customer_code) && in_array( 'guest', $user->roles ) ) {
$order->update_meta_data( 'ipx_customer_code', esc_attr($user->ipx_customer_code) ); // Set the Ipx code
$order->update_meta_data( 'guest_user_id', int_val($user->ID) ); // set guest user ID (useful)
$order->save(); // save new meta data
}
}
Code goes in functions.php file of your active child theme (or active theme). Untested.
Now if it doesn't work, we need to get that later on, when payment is processed and order gets an order status.
So in that case, let's try something similar using a different hook:
add_action( 'woocommerce_order_status_changed', 'link_guest_ipx_code_to_the_order', 10, 4 );
function link_guest_ipx_code_to_the_order( $order_id, $status_from, $status_to, $order ) {
// Check that no "IPX customer coode is assigned
if ( ! $order->get_meta('ipx_customer_code') ) {
// Get the WP_User object from the billing email
$user = get_user_by( 'email', $order->get_billing_email() );
// Check that user exist and has an "ipx customer code" and has a guest user role
if ( $user && isset($user->ipx_customer_code) && in_array( 'guest', $user->roles ) ) {
$order->update_meta_data( 'ipx_customer_code', esc_attr($user->ipx_customer_code) ); // Set the Ipx code
$order->update_meta_data( 'guest_user_id', intval($user->ID) ); // set guest user ID (useful)
$order->save(); // save new meta data
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Untested.
So here we are able to add the "IPX customer code" (and the "guest user ID") to each order only for "guest" user role.
Upvotes: 1