I need update WordPress account email with woocommerce billing email after successful checkouts. I used this code but it does not work :
/* Update account email based on woocommerce billing email */
add_filter( 'woocommerce_thankyou' , 'custom_update_checkout_fields', 10, 2 );
function custom_update_checkout_fields($user_id, $old_user_data ) {
$current_user = wp_get_current_user();
// Updating Billing info
if($current_user->user_email != $current_user->billing_email)
update_user_meta($user_id, 'billing_email', $current_user->user_email);
}
Am I used an outdated code?
There are some mistakes. Try the following instead:
add_filter( 'woocommerce_thankyou' , 'thankyou_update_wordpress_user_email' );
function thankyou_update_wordpress_user_email( $order_id ) {
$order = wc_get_order( $order_id );
$user = $order->get_user();
$billing_email = $order->get_billing_email();
// Updating user account email
if( is_a($user, 'WP_User' ) && $user->user_email != $billing_email ) {
$user_data = wp_update_user( array( 'ID' => $user->ID, 'user_email' => $billing_email ) );
}
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Note: When changing user email, WordPress Send an email to the new user email.
Related
I get several orders where a customer selects "Direct Bank Transfer" and then they change their mind and want to pay by Credit Card. This is quite annoying because I have to manually change the order from "On Hold" to "Pending Payment" so they can pay by card via the "order-pay" endpoint which is found in "My Account" under "Orders".
I've been using the WooCommerce change order status BACS processing to automatically change the order status from "On Hold" to "Pending Payment".
// WooCommerce Change Order Status BACS Pending
add_action( 'woocommerce_thankyou', 'bacs_order_payment_pending_order_status', 10, 1 );
function bacs_order_payment_pending_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order object
$order = new WC_Order( $order_id );
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ('on-hold' == $order->status ) ) {
$order->update_status('pending');
} else {
return;
}
}
But since I have several user profiles (I sell B2B as well), this is not practical for my shop. I'm trying to expand this snippet to also check for the user role. I've used the following in my other snippets. Is it possible to add the below logic to the snippet above?
$user = wp_get_current_user();
$roles = (array) $user->roles;
$roles_to_check = array('administrator', 'customer', 'shop_manager');
$compare = array_diff($roles, $roles_to_check);
if (empty($compare)){
This is my attempt.
// WooCommerce Change Order Status BACS Pending
add_action( 'woocommerce_thankyou', 'bacs_order_payment_pending_order_status', 10, 1 );
function bacs_order_payment_pending_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order object
$order = new WC_Order( $order_id );
$user = wp_get_current_user();
$roles = (array) $user->roles;
$roles_to_check = array('administrator', 'customer', 'shop_manager');
$compare = array_diff($roles, $roles_to_check);
if (empty($compare)){
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) && ('on-hold' == $order->status ) ) {
$order->update_status('pending');
} else {
return;
}
}
You can use this as follows, comment with explanation added in the code
function bacs_order_payment_pending_order_status( $order_id ) {
// Get $order object
$order = wc_get_order( $order_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Get user
$user = $order->get_user();
// Roles
$roles = (array) $user->roles;
// Roles to check
$roles_to_check = array( 'administrator', 'customer', 'shop_manager' );
// Compare
$compare = array_diff( $roles, $roles_to_check );
// Result is empty
if ( empty ( $compare ) ) {
if ( $order->get_payment_method() == 'bacs' && $order->has_status( 'on-hold' ) ) {
$order->update_status( 'pending' );
}
}
}
}
add_action( 'woocommerce_thankyou', 'bacs_order_payment_pending_order_status', 10, 1 );
Might Come in handy: WooCommerce: Get Order Info (total, items, etc) From $order Object
Woocommerce version 3.4.0 has introduced a much better hook that allows to change the default status for BACS payment gateway which is set to "on-hold".
Using this hook will:
Lighten your code,
Avoid "on-hold" notification to the customer when a BACS order is placed.
Here is that code:
add_filter( 'woocommerce_bacs_process_payment_order_status','filter_process_payment_order_status_callback', 10, 2 );
function filter_process_payment_order_status_callback( $status, $order ) {
// Here set the user roles to check
$roles_to_check = array( 'administrator', 'customer', 'shop_manager' );
$user = $order->get_user(); // Get the WP_User Object
$compare = array_diff( $user->roles, $roles_to_check ); // compare
if ( empty ( $compare ) ) {
return 'pending';
}
return $status;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Since WooCommerce 5+: Allow re-sending New Order Notification in WooCommerce 5+
Enabling New Order email notification (sent to the admin) for BACS payments:
As pending Orders doesn't send email notifications, you can enable that with the following
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Only for "pending" order status and BACS payments
if( $order->has_status( 'pending' ) && $order->get_payment_method() === 'bacs' )
{
// Send "New Email" notification (to admin)
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related: Send an Email notification to the admin for pending order status in WooCommerce
Useful: How to get WooCommerce order details
Related: Change default WooCommerce order status to processing for cheque and bacs payments
Freshly updated answer thread: WooCommerce change order status BACS processing
I want to be able to change who receives the Woocommerce email notifications based on what role the user is when ordering.
For example, If the user is logged in as a Wholesale Customer then a different email will be notified.
I've found how to change it when a new order is complete using the woocommerce_email_recipient_new_order hook but i can't find any hooks related to the failed or cancelled notifications.
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
function sv_conditional_email_recipient( $recipient, $order ) {
// Bail on WC settings pages since the order object isn't yet set yet
// Not sure why this is even a thing, but shikata ga nai
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
}
// just in case
if ( ! $order instanceof WC_Order ) {
return $recipient;
}
if ( in_array( 'wholesale_customer', (array) $user->roles ) ) {
$recipient .= ', shaun#example.com';
return $recipient;
}
return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
Can anyone help please?
The hook you are already using is a composite hook: woocommerce_email_recipient_{$this->id}, where {$this->id} is the WC_Email ID like new_order. So you can set any email ID instead to make it work for the desired email notification.
Below You have the 3 hooks for "New Order", "Cancelled Order" and "Failed Order" that you can use for the same hooked function.
In your function, I have removed some unnecessary code and completed the code to get the customer data (the user roles) related to the order:
add_filter( 'woocommerce_email_recipient_new_order', 'user_role_conditional_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_cancelled_order', 'user_role_conditional_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'user_role_conditional_email_recipient', 10, 2 );
function user_role_conditional_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Get the customer ID
$user_id = $order->get_user_id();
// Get the user data
$user_data = get_userdata( $user_id );
// Adding an additional recipient for a custom user role
if ( in_array( 'wholesale_customer', $user_data->roles ) )
$recipient .= ', shaun#example.com';
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
I am trying to send an email to the client when an order gets cancelled. By default, woocommerce only sends this email only to the admin of the site.
This code has solved the issue for related posts on the web:
function wc_cancelled_order_add_customer_email( $recipient, $order ){
return $recipient . ',' . $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
However, it seems like woocommerce removed those filter hooks completely.
Is there any way of doing this?
Thanks in advance!
In this custom function hooked in woocommerce_order_status_changed action hook, I am targeting "cancelled" and "failed" orders sending an the corresponding email notification to the customer (as admin will receive it on his side by WooCommerce automated notifications):
add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
if ( $new_status == 'cancelled' || $new_status == 'failed' ){
$wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
$customer_email = $order->get_billing_email(); // The customer email
}
if ( $new_status == 'cancelled' ) {
// change the recipient of this instance
$wc_emails['WC_Email_Cancelled_Order']->recipient = $customer_email;
// Sending the email from this instance
$wc_emails['WC_Email_Cancelled_Order']->trigger( $order_id );
}
elseif ( $new_status == 'failed' ) {
// change the recipient of this instance
$wc_emails['WC_Email_Failed_Order']->recipient = $customer_email;
// Sending the email from this instance
$wc_emails['WC_Email_Failed_Order']->trigger( $order_id );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should works in WooCommerce 3+
If you need, instead of changing the email, you can add it, to existing recipients:
// Add a recipient in this instance
$wc_emails['WC_Email_Failed_Order']->recipient .= ',' . $customer_email;
Related answer: Send an email notification when order status change from pending to cancelled
I'm looking for a way to hide the billing address on the checkout page of my woocommerce theme if the user has already filled up the billing form (from a previous order or if the user has done it previously from the "my account" page).
I've found ways to hide the billing / shipping form completely on the checkout page if the user is logged in (see below), however I can't find a way to do the above.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
if( is_user_logged_in() ){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
Any idea?
Thank you!
It will depend what you consider to be a fully completed address i made snippet function you can use to go further with.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
if( is_user_logged_in() && !has_billing()){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
// Check the meta of Postcode and Country if they are entered.
function has_billing($user_id = false){
if(!$user_id)
$user_id = get_current_user_id();
$shipping_postcode = get_user_meta( $user_id, 'billing_postcode', true );
$shipping_country = get_user_meta( $user_id, 'billing_country', true );
// Fetch more meta for the condition has needed.
if($shipping_postcode && $shipping_country){
return true;
}
return false;
}
Note: the prefix shipping_ there is one for billing ( billing_ ).
Edit: Here the meta key billing_address_1 and billing_address_2 always the prefix can be either billing_ or shipping_
Also if for some reason you don't have a shipping or billing address associated in the user meta keys but the customer once did an order you can check this code to fetch order address.
Woocommerce WC_Order get_shipping_address() not returning as array (old post might not be valid anymore)
I have a user role for wholesale customers (wholesale_customer). When I mark an order as completed a notification is send to customers. This is ok for my regular customers but I would like to disable/remove the notification for wholesale customers.
What I've got so far:
function do_not_send_some_email_notifications(WC_Emails $wc_emails) {
$order = new WC_Order( $order_id );
if ( $order->user_id > 0 ) {
$user_id = $order->user_id;
$get_user_data = get_userdata($user_id);
$user_roles = $get_user_data->roles;
if (in_array('wholesale_customer', $user_roles)) {
remove_action('woocommerce_order_status_completed_notification', array($wc_emails->emails['WC_Email_Customer_Completed_Order'], 'trigger'));
}
}
}
add_action('woocommerce_email', 'do_not_send_some_email_notifications');
I have tested this but it's not working.
It would be very nice if someone can point me in the right direction.
Thanks.
Updated 2: I finally find the right hook to make it work. I have revisited your code a little bit using a very similar custom function hooked in the woocommerce_order_status_completed action hook.
Here is the code:
function custom_conditional_email_notifications( $order_id ) {
// Set HERE the targetted user role
$targeted_user_role = 'wholesale_customer';
// Get the order object, the user ID, and the user role.
$order = wc_get_order($order_id);
$user_id = $order->get_user_id();
$user_info = get_userdata($user_id);
if ( in_array( $targeted_user_role, $user_info->roles ) && $user_id > 0 )
remove_action( 'woocommerce_order_status_completed_notification', array(
$wc_emails->emails['WC_Email_Customer_Completed_Order'],
'trigger'
) );
}
add_action( 'woocommerce_order_status_completed', 'custom_conditional_email_notifications' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.