Changing in WooCommerce email notification Order Item table "Product" label - php

I need to change the text (label) "Product" to "Ticket" in WooCommerce Order Item table email notifications.
How would I do this?
Is it possible?
Thanks

First we need to get the Email ID to target the all email notifications. The only way is to get it before and to set the value in a global variable.
Then in a custom function hooked in Wordpress gettext action hook, we can change (translate) "Product" in all email notifications.
Here is that code:
## Tested on WooCommerce 2.6.x and 3.0+
// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
$GLOBALS['email_id_str'] = $email->id;
}
add_filter('gettext', 'wc_renaming_email_label', 50, 3);
function wc_renaming_email_label( $translated_text, $untranslated_text, $domain ) {
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
if( !is_admin() && $email_id ) {
if( $untranslated_text == 'Product' )
$translated_text = __( 'Ticket', $domain );
}
return $translated_text;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce from 2.6.x to 3.0+ and works.

If you don't want to modify the WooCommerce files, use this plugin https://wordpress.org/plugins/woo-custom-emails/
If you want to edit it from the WooCommerce files, then modify the email templates in /wp-content/plugins/woocommerce/templates/emails/

Related

Add custom item data after item name on Woocommerce order emails

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.

Different email heading based on shipping methods in Woocommerce

So I want to send the same email with a slightly different heading in WooCommerce. It uses the argument $email_heading variable to hold the value of that current email heading so I figure a simple replacement would work. It doesn't. I could be completely missing the mark here any help is much appreciated.
I want it to say Your Order is ready for pickup when they choose local pickup and then the default value (stored the email settings of woocommerce) when any other shipping is chosen.
add_filter( "woocommerce_email_heading_customer_completed_order", 'HDM_woocommerce_email_header', 10, 2 );
function HDM_woocommerce_email_header( $email_heading, $email ) {
if ('customer_completed_order' == $email->id && $order->has_shipping_method('local_pickup') ){
$order_id = $order->get_id();
$email_heading = '<h1>Your Order Is Ready For Pickup</h1>';
}
return $email_heading;
};
There are some mistakes in your code like $email that is in fact $order. Also you are already targeting customer_completed_order in this composite hook, so you don't need it in your IF statement…
So try instead:
add_filter( "woocommerce_email_heading_customer_completed_order", 'custom_email_heading', 10, 2 );
function custom_email_heading( $email_heading, $order ) {
if ( $order->has_shipping_method('local_pickup') ){
$email_heading = '<h1>Your Order Is Ready For Pickup</h1>';
}
return $email_heading;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
i'm wondering if this is valid for email subject:
add_filter( "woocommerce_email_subject_customer_completed_order", 'custom_email_subject', 10, 2 );
function custom_email_subject( $subject, $order ) {
if ( $order->has_shipping_method('local_pickup') ){
$subject = 'Your Order Is Ready For Pickup';
}
return $subject;
}

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 );

Display a custom field value in Woocommerce email notification template

In WooCommerce, I am using "WC Fields Factory" plugin to create a 'serial' custom field. I need to display its value in /woocommerce/emails/customer-completed-order.php template file.
I have tried to use:
echo get_post_meta(get_post()->ID, "wccaf_serial", true );
But it does not work.
What I am doing wrong?
Thanks
You can use directly $order with all WC_Order methods in this email template, to get the Order ID, this way:
// Get the Order ID (WooCommerce retro-compatibility)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Get "serial" custom field value
$serial = get_post_meta($order_id, "wccaf_serial", true );
// Display "serial" custom field value
echo '<p>'.__('Serial', 'woocommerce') . $serial . '</p>';
Please read: Template Structure + Overriding Templates via a Theme
Also instead of overriding this template, you can use any available hook for example like:
add_action( 'woocommerce_email_order_details', 'action_wc_email_order_details' 50, 4 );
function action_wc_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
// Get the Order ID (WooCommerce retro-compatibility)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Get "serial" custom field value
$serial = get_post_meta($order_id, "wccaf_serial", true );
// Display "serial" custom field value
echo '<p>'.__('Serial', 'woocommerce') . $serial . '</p>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works in WooCommerce 2.6.x or 3+
Have a look at WP HTML Mail. This plugin has an integrated mailbuilder for WooCommerce emails. You can edit the content of your mail in WordPress editor and add custom fields from menu "Placeholder".
Here are some screenshots: http://wp-html-mail.com/woocommerce-custom-email-content/

Categories