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 );
Related
In WooCommerce, I would like to add a new custom field to order details. I now that I can use use the code below to create a new custom field 'referenceNumber' and adds in it "ordercreated" value:
update_post_meta($order_id, 'referenceNumber', 'ordercreated']);
What I would like is to make that through checkout once an order is placed.
But it doesn't work it doesn't add a new custom field to order details page and don't add the value 'ordercreated', as you can see in this screenshot:
So the question is how to add a custom field when an order is placed in WooCommerce?
To add a custom field to an order you can use:
WordPress update_post_meta() function (from an order id):
$order_id = $order->get_id(); // If needed
update_post_meta($order_id, 'referenceNumber', 'ordercreated'); // add and save the custom field
WooCommerce WC_Data update_meta_data() method (from the order object or the order id):
$order = wc_get_order( $order_id ); // If needed: Get the WC_Order object from the order Id
update_meta_data('referenceNumber', 'ordercreated'); // Add the custom field
$order->save(); // Save the data
Where referenceNumber is the meta key and ordercreated is the meta value. Both works.
Now to add a custom field to an order when customer place an order, you can use:
woocommerce_checkout_create_order action hook (before order data is saved - used to adjust order data before it's saved):
add_action( 'woocommerce_checkout_create_order', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' );
}
woocommerce_checkout_update_order_meta action hook (order already exist - used to add custom meta data):
add_action( 'woocommerce_checkout_create_order', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order_id, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' );
}
woocommerce_checkout_order_created action hook (order already exist - to trigger an action or also to add custom meta data):
add_action( 'woocommerce_checkout_order_created', 'add_custom_field_on_placed_order', 10, 2 );
function add_custom_field_on_placed_order( $order_id, $data ){
$order->update_meta_data( 'referenceNumber', 'ordercreated' ); // Add the custom field
$order->save(); // Save data (as order exist yet)
}
Or:
add_action( 'woocommerce_checkout_order_created', 'add_custom_field_on_placed_order' );
function add_custom_field_on_placed_order( $order ){
update_post_meta($order->get_id(), 'referenceNumber', 'ordercreated');
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Hi i had a similar requirement few days ago, as i needed to add new field at checkout. Following article helped me. You can check it too. Here is the link. Basically you will have a write a function which will use woo commerce hook "woocommerce_default_address_fields"
https://wisdmlabs.com/blog/add-custom-fields-woocommerce-checkout-page/
I'm trying to add some labels after Woocommerce order email items.
Ex- we can do that on cart by using this filer
add_filter( 'woocommerce_cart_item_name', array( $this, 'wc_esd_show_date_cart_page' ), 10, 2 );
above call wc_esd_show_date_cart_page() function and show its data after item name
I want to do the same for order email items, anyone know there have some filter for it, or another way to do this.
The very similar hook for orders and email notifications is woocommerce_order_item_name filter hook.
But the function arguments are quiet different and you might need to make some changes to your existing function code.
The following will target only email notifications
add_filter( 'woocommerce_order_item_name', 'wc_esd_show_date_on_order', 10, 3 );
function wc_esd_show_date_on_order( $item_name, $item, $is_visible ) {
// Only for email notifications
if( is_wc_endpoint_url() )
return $item_name;
// ---------------- Your code start below ---------------- //
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
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();
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.
I am very new to WordPress, and have created an e-commerce store with WooCommerce.
After the customer places an order, I get an email and the customer get an email- one for me to say what they have ordered, and one to them as a thank you email.
Within this thank you email, in my functions.php file, I have learned to change the subject of the header to include their name such as this:
//add the first name of the person to the person getting the reciept in the subject of the email.
add_filter('woocommerce_email_subject_customer_processing_order','UEBC_change_processing_email_subject', 10, 2);
function UEBC_change_processing_email_subject( $subject, $order ) {
global $woocommerce;
$subject = 'Thanks for your ' . get_bloginfo( 'name', 'display' ) . ' Order, '.$order->billing_first_name .'!';
return $subject;
}
The code snippet above works correctly, and is only displayed to the customer, not to me. e.g. "thanks for your order ABCD Clothes order John!".
Within the body of the email, I am trying to make this personal as a small thank you message, however, when I make the message, I am using the hook:
add_action( 'woocommerce_email_before_order_table', 'custom_add_content', 20,1 );
I know that since im using the woocommerce_email_before_order_table hook, the custom function will be send in the body of the email to both the customer and myself.
I was wondering, is there a hook that Woocommerce provides so that the custom function will only be sent to the customer within the body of the email?
For example: woocommerce_email_header_customer_processing_order or words to that effect?
Thanks
To add some custom content using woocommerce_email_before_order_table hook and targeting just the customer "processing order" email notification, you should try this:
add_action( 'woocommerce_email_before_order_table', 'custom_content_to_processing_customer_email', 10, 4 );
function custom_content_to_processing_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
if( 'customer_processing_order' == $email->id ){
// Set here as you want your custom content (for customers and email notification related to processing orders only)
echo '<p class="some-class">Here goes your custom content… </p>';
}
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
This code is tested and works