Get customer order count in new order email notification - php

I'd like to indicate in WooCommerce "new order" email notification, if it's a repeat customer.
It seems simple, but I've tried about 5 different methods and none worked. I've tried putting this into 2 different hooks:
woocommerce_email_after_order_table
woocommerce_email_subject_new_order.
Seems like wc_get_customer_order_count($user->ID) should work but it appears that the $user object is not passed into those hook's functions, right?
I'm also wondering if this is possible when it's a guest and not a registered user, maybe by comparing the email address?
Thanks

WooCommerce Email notifications are related to orders.
In woocommerce_email_after_order_table hook, you have the Order object as an argument in your hooked custom function and also the $email object.
With that $order object, you can get the user ID this way:
$user_id = $user_id = $order->get_user_id();
From the $email object you can target the new order email notification.
So the working code is going to be:
add_action( 'woocommerce_email_after_order_table', 'customer_order_count', 10, 4);
function customer_order_count( $order, $sent_to_admin, $plain_text, $email ){
if ( $order->get_user_id() > 0 ){
// Targetting new orders (that will be sent to customer and to shop manager)
if ( 'new_order' == $email->id ){
// Getting the user ID
$user_id = $order->get_user_id();
// Get the user order count
$order_count = wc_get_customer_order_count( $user_id );
// Display the user order count
echo '<p>Customer order count: '.$order_count.'</p>';
}
}
}
You can also use instead the woocommerce_email_before_order_table hook for example…
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.

Related

How to get the order information inside the customer creation hook?

When a visitor buys a new product in my WooCommerce shop, I'm using the woocommerce_created_customer hook to create a new user on an external database with some information I get from the $customer_id, $new_customer_data, $password_generated arguments.
For example:
function action_woocommerce_created_customer($customer_id, $new_customer_data, $password_generated) {
// Create a new user on external database
}
add_action('woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3);
Well. What I need is a connection to the current order to get the order id. In my shop, a user account can only be created in combination with an order. Is there a way to get order information inside the woocommerce_created_customer hook? Or is this do_action just called before the order is done?
I thought about using another hook after the payment is done. But this is not possible in my case because this is the only hook I found which I can get the unhashed user password, which I very need.
Do you have an idea how to get the order information inside the customer creation hook?
The order is created after the customer.
$this->process_customer( $posted_data );
$order_id = $this->create_order( $posted_data );
$order = wc_get_order( $order_id );
Use this hook to do the stuff.
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );
$posted_data contains the username and password
$username = ! empty( $posted_data['account_username'] ) ? $posted_data['account_username'] : '';
$password = ! empty( $posted_data['account_password'] ) ? $posted_data['account_password'] : '';

Use user phone number as auto generated password

I want a code that the password generated by woocommerce will be taken from the phone field on the checkout page when the user fills in the details for the billing
I tried the following code and it did not work
I would love a code that works. thanks.
<?php
//Use user phone number as auto-generated password
function wcs_filter_password_phone_number( $args ) {
$args['user_pass'] = $args['dbem_phone'];
return $args;
}
add_filter( 'woocommerce_new_customer_data', 'wcs_filter_password_phone_number' );
It's not really secure setting the password based on the customer's phone number. Anyone could find a way in to be honest.
Having said that, the task is interesting, and based on WooCommerce: Update Order Field Value After a Successful Order it is possible indeed to alter an order/user field after a successful checkout.
You could hook into "woocommerce_thankyou" as per the article, or even into "woocommerce_payment_complete" if you wanted to do that in the background and only for paid/completed orders.
Also, you should look into https://developer.wordpress.org/reference/functions/wp_set_password/ which is the WP function to set a new password programmatically.
Here's my attempt:
add_action( 'woocommerce_thankyou', 'bbloomer_alter_password_after_order' );
function bbloomer_alter_password_after_order( $order_id ) {
$order = wc_get_order( $order_id );
$phone = $order->get_billing_phone();
$user_id = $order->get_user_id();
if ( $phone && $user_id ) {
wp_set_password( $phone, $user_id );
}
}

Conditional custom New Order Woocommerce email notification on different recippients

Okay I have had a question come from a client that doesn't seem impossible, but correctly setting up the conditions is a problem for me. Here is what is happening and what I need. i would really this to be a function of possible.
The store has a user hierarchy. There are 2x Super Admins (ID: admin), 5x Admins (ID: admin2), 15x Bank Customers (ID: banks), and 2x Real Estate customers (ID: real estate). I have already setup the custom users with the ID's. No products have pricing. Everything is invoiced in an email with the order_info. Everything is paid at a later date in house. So the site really relies on emails.
If the 15x Bank Customers place a new_order, these new_order(s) need to send the email invoice to the 5x Admins.
If the 2x Real Estate customers place a new_order, these new_order(s) need to send the email invoice to the 5x Admins.
If the 5x Admins place a new_order, these new_order(s) need to send the email invoice to the 2x Super Admins.
I am not the greatest with php and what I had previous was very sloppy.
Any help?
You can use wc hook as follows in functions.php filr
add_action( 'woocommerce_new_order', 'send_email_to_admin', 1, 1 );
function send_email_to_admin($order_id) {
// use order id to find user id and send email
}
Using a custom function hooked in woocommerce_thankyou action hook, will allow you to make that conditional email notifications based on user roles and on custom multiple recipients.
You will need to replace admins and super admins email in this function.
You will need also to check that the users roles are matching in the 3 if statements… The ID for Real estate can't have a space normally and should be instead: 'real_estate'
This will send email notifications for orders that have a status like 'on-hold', 'pending', 'processing' or 'completed'...
Once this custom email will be triggered a custom field '_custom_emails_sent' will be set for the order.
Here is the code:
add_action( 'woocommerce_thankyou', 'custom_new_order_email_notifications', 10, 1 );
function custom_new_order_email_notifications( $order_id ){
// If Custom Emails already sent we exit
if( get_post_meta( $order_id, '_custom_emails_sent', true ) ) return;
$targeted_statuses = array( 'wc-on-hold', 'wc-pending', 'wc-processing', 'wc-completed' );
$order_status = get_post_status( $order_id );
// Only for the correct order statuses; If not we exit
if( ! in_array( $order_status, $targeted_statuses ) ) return;
// HERE (below) replace super admins and admins REAL emails
$super_admin_emails = array(
'supadmin1#example.com', 'supadmin2#example.com' );
$admin_emails = array(
'admin1#example.com', 'admin2#example.com',
'admin3#example.com', 'admin4#example.com', 'admin5#example.com' );
// Get the user of the order
$user_id = get_post_meta( $order_id, '_customer_user', true );
$user = get_userdata( $user_id );
$recipient = '';
// 1. Bank Customers user role
if( in_array('banks', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// 2. Real estate Customers user role
if( in_array('real_estate', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// 3. Admins Customers user role
if( in_array('admin2', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// Sending new order email notification to the targeted recipients
if( '' != $recipients ){
$mailer = WC()->mailer()->get_emails();
$mailer['WC_Email_New_Order']->recipient = $recipients;
$mailer['WC_Email_New_Order']->trigger( $order_id ); // sending
// We set a custom field that will avoid repetitive sends
update_post_meta( $order_id, '_custom_emails_sent', '1' );
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works

Add Applied Coupon Code in Admin New Order Email Template - WooCommerce

Let me clear my question:
I have downloaded & activated WooCommerce Plugin for E-Commerce Functionality.
I want to add "Applied coupon code" in Admin New Order Email Template using my custom plugin.
Now:
Can you tell me that exact Hook or Function which is actually setting up that New Order Email Template so that i will override it?
Can you tell me how to call applied coupon code, so that i will display this in email template?
It would be great, if you help me please.
This can be done using a custom function hooked in woocommerce_email_order_details action hook (for example) that will display in admin emails notifications the used coupons in the order:
// The email function hooked that display the text
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;
foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
}
// For multiple coupons
else {
$coupon_codes = implode( ', ', $coupon_codes);
echo '<p>'.__( 'Coupons Used: ').$coupon_codes.'<p>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works...
This is because $coupon_codes is not an array() define it with $coupon_codes=array();

Which hook to add custom data In New Order Admin Woocommerce email

In WooCommerce, How and where I can apply some code for New Order Email notification, where I should get the Applied Coupon Code display.
In the template for New Order Email notification I have #hooked WC_Emails::order_details() that shows the order details table…
Also is WC_Email::order_details() the exact hook which I am searching for to update coupon code in New order mail to admin?
I am loosed, any help will be really appreciated…
The hook that you are searching is woocommerce_email_order_details action hook that can be used this way:
add_action( 'woocommerce_email_order_details', 'action_email_order_details', 10, 4 );
function action_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
if( $sent_to_admin ): // For admin emails notification
// Your code goes HERE
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
In your function you can use any of the 4 arguments from the hook like $order the WC_Order object.
You can use the WC_Abstract_Order method get_used_coupons() that will give you an array of coupons codes used in the order, like:
// Get the array of coupons codes
$coupon_codes = $order->get_used_coupons();
// Convert and display the array in a coma separated string:
echo 'Coupon codes: ' . implode( ', ', $coupon_codes );

Categories