woocommerce set billing first name - php

I'm trying to find a woocommerce method that will allow me to set the billing and or shipping first name.
The customer class will allow me to set some fields but not first_name. Anyone know if a method exists or do I need to just update the wp_usermeta table directly using billing_first_name.
Here is an example of what doesn't work:
global $woocommerce;
$user_first_name = get_user_meta( $user_id, 'first_name', true );
if( !empty( $woocommerce->customer->get_billing_first_name() ) ) {
$woocommerce->customer->set_billing_first_name($user_first_name); // a set_billing_first_name method doesn't exist
}

Customer in woocommerce is nothing but a Wordpress user. If you look at WC_Checkout->process_checkout() is creates a new user and then sets billing_first_name as first_name.
So to update the billing_first_name update the first_name using update_user_meta.
Hope this helps!

Related

How to get user roles during checkout and store them as user meta?

What I a looking for, is to get the user's roles and store them as user meta after the checkout process.
To accomplish that I was trying by using this function:
add_action( 'user_register', 'set_user_registration_role', 10, 1 );
function set_user_registration_role( $user_id ) {
$user = wp_get_current_user( $user_id ); // getting & setting the current
user
$roles = ( array ) $user->roles; // obtaining the role
// Update the registration meta data
update_user_meta ( $user_id, 'registration_role', $user->user_registered );}
Unfortunately, this is not working, I'm getting the user meta "registration_role" but it is empty and not
showing the user's roles.
Is there anyone who could help me with this?
Thanks in advance!

Programmatically created WooCommerce order - set customer based on email instead of user ID [duplicate]

This question already has an answer here:
Programmatically created WooCommerce order have no tax for new users
(1 answer)
Closed 2 years ago.
Im creating a WooCommerce order programmatically trough a form and I link the order trough the user id:
$order->set_customer_id( $user_id );
I retrieve the customer information via mail instead of ID:
$user = get_user_by( 'email', $email );
Is it possible to link the order to an existing customer via the existing email instead of the user id? Something like this:
$order->set_customer_id( $email );
Is this possible?
Function get_user_by() returns WP_User object, so you only need to take the ID from the object and substitute it into the function set_customer_id() from $order:
// $user - it is WP_User object
$user = get_user_by( 'email', $email );
$order->set_customer_id( $user->ID );
Why would you need to link the order to the customer email when you have his userId? That gives you access to the user object real quick and from there you can access whatever you want. For example $user->user_email would give you his email
$user = new WP_User($user_id);
$user_email = $user->user_email;
Another option is that you retrieve those info from the Woocommerce default billing fields, which you should fill (even when creating it programmatically) and you can access those with:
$order = new \WC_Order($id);
$order->get_billing_email();
Least but not last, save it in any postmeta field of your choice:
update_post_meta($order_id, 'my_email_custom_field', $email);

What hook to be used for cancelled booking in WooCommerce Bookings?

I'm trying to make an SQL query to run upon cancellation of a booking, via a custom plugin, updating custom user meta data.
Here's my code:
function wporg_callback() {
global $wpdb;
$wpdb->query("UPDATE usermeta SET meta_value = 15 WHERE umeta_id = 131");
}
add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback');
But It's not working.
Is there something wrong with the query? Is the right action not being used?
Edit - I Also tried the following without success:
add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback');
function wporg_callback( $booking_id ) {
// Add/Update custom user meta data
update_user_meta( 2, 'download_credits', 17 );
}
Updated:
The correct hook to be used is woocommerce_booking_cancelled (a composite hook) that will allow you to retrieve the user ID and to Add/update custom user meta data like below:
add_action('woocommerce_booking_cancelled', 'booking_cancelled_transition_to_callback', 10, 2 );
function booking_cancelled_transition_to_callback( $booking_id, $booking ) {
// Get the user ID from the Booking ID
$user_id = get_post_field ('post_author', $booking_id);
$download_credits = get_post_meta( $booking_id, '_booking_cost', true );
// Add/Update custom user meta data
update_user_meta( $user_id, 'download_credits', $download_credits );
}
Code goes in functions.php file of your active child theme (or theme). Tested and works.
How to get the booking data (from the booking ID displayed in the order):
Go to your database under wp_postmeta table to get the desired meta_key from the post_id (that is the booking ID)
Use that meta key in get_post_meta() WordPress function like:
$meta_value = get_post_meta($booking_id, 'the_meta_key', true);
Notes:
An SQL query is not needed to add/update custom user meta data, as you can use instead the dedicated Wordpress function update_user_meta().
I couldn't make it work using woocommerce_bookings_cancelled_booking hook.
WooCommerce Bookings action and filters hooks developer documentation

woocommerce admin create order get customer id on create

It's been a couple of days now, and I can't seem to find a hook for adding/updating user meta
add_user_meta( 'user_id', 'custom_key', 'custom_value');
when creating an order in woocommerce admin (woocommerce->orders->add order), backend.
Using
add_action('woocommerce_process_shop_order_meta', 'admin_process_shop_order', 10, 1);
This works fine for doing things while the order is processed. However, I need to get the customer ID, which from what I can tell doesn't exist until the order is actually created (makes sense).
So my question is, what hook (or other solution) can I use to get the customer ID once the order is created and searchable with
get_post_meta($order_id, '_customer_user', true);
Thank you #Gugan for your suggestions! It looks like that with your help I was finally able to get this mess sorted :)
Since I only wanted this to fire one time, i.e. when the order is created (and not again when updated), I hade to combine two actions.
First 'woocommerce_process_shop_order_meta'. Here I can check if the post meta exists (if it does, the order has already been created and should be left alone)
function check_order($post_id){
$new_order = get_post_meta($post_id, '_customer_user', true);
if(!$new_order){
add_action('woocommerce_order_status_[MY_CUSTOM_ORDER_STATUS]-processing', 'total_count');
}
}add_action('woocommerce_process_shop_order_meta', 'check_order', 10, 1);
If this is a new order move on to 'woocommerce_order_status_[MY_CUSTOM_ORDER_STATUS]-processing' (with my function 'total_count')
function total_count($post_id){
$order = wc_get_order($post_id);
$customer_id = $order->get_user_id();
$user_role = get_user_meta($customer_id, 'wp_capabilities', true);
$custom = serialize(array('[MY_CUSTOM_USER_ROLE]' => true));
$today = date('Y-m-d');
if($user_role = $custom){
$current_total = get_user_meta($customer_id, 'total', true);
$increment_total = $current_total+1;
update_user_meta( $customer_id, 'total', $increment_total);
update_user_meta( $customer_id, 'last', $today);
}
}
Now I only get an increment on my custom user metas "total" and "last" if this is a new order and if the customer is of my custom user role. Another plus to this is that it will only work for one order status (i.e. in my case [MY_CUSTOM_ORDER_STATUS]-processing).
Just jotting down my solution here for anyone else looking to handle similar custom order creation work.

Add a new field to woocommerce_email_customer_details or any other part of emails

Ok so I am building a woocommerce site for a client and they love it but the only issue is they need a customer number to come through with the email for all new orders. They are willing to go in and fill in this info for each user, but I cannot find out how to get this info to automatically send with the new order email.
I am using WP - Members plugin to add custom information to the users profile, and have created a field there called Customer # (option name: customer_number) I cannot figure how to get woocommerce to add this number to the email that is sent when they place the order (to the admin)
I have tried many different things, it seems like it should just be a hook but I dont know how to get the values or have not been able to figure this out yet in any way.
I have looked and looked and this is my last resort, my minimal understanding of php and hooks is probably my downfall here, so please be very clear if instructing me in that way.
I will not give up on this issue, so I hope someone can help!
We can adapt from my tutorial on customizing the checkout fields to display some extra info in the email:
function kia_display_email_order_user_meta( $order, $sent_to_admin, $plain_text ) {
$user_id = $order->customer_user;
if( ! empty( $user_id ) && ( $data = get_user_meta( $user_id, '_some_user_meta', true ) ) != '' ){
if( $plain_text ){
echo 'The user data for some field is ' . $data;
} else {
echo '<p>The user data for <strong>some field</strong> is ' . $data . '</p>';
}
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_user_meta', 30, 3 );
In this case we'll replace the get_post_meta() of my tutorial with get_user_meta().... and knowing that the user Id is stored in the $order->customer_id property of the $order object.

Categories