Display a custom field value in Woocommerce email notification template - php

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/

Related

Display custom payment field in Woocommerce Admin, Orders and emails

I need to display my custom checkout fields in admin orders page, thank_you and email notifications.
I am using Validate and save additional checkout field for specific payment gateway in Woocommerce answer code to show, validate and save my custom field.
From Woocommerce display custom field data on admin order details I am trying to display my custom field saved inputted value.
This is the code I have so far:
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address',
'show_Ean_nummer_in_admin', 10, 1 );
function show_Ean_nummer_in_admin ( $order ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value on the order emails
add_action( 'woocommerce_email_order_details', 'ean_in_emails' 50, 1 );
function ean_in_emails( $order, $sent_to_admin, $plain_text, $email ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value in thank you
add_action( 'woocommerce_thankyou', 'ean_in_thankyou' );
function ean_in_thankyou() {
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
But It's not working. The field does get appended to the database, but does not display anywhere:
How can I display the field properly?
The following code will display your "Udfyld EAN" custom field value in orders and email notifications:
1) To display that in Woocommerce order admin single pages:
// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_field_admin_display_order_meta', 10, 1 );
function custom_field_admin_display_order_meta( $order ){
$business_address = get_post_meta( $order->get_id(), 'Business Address?', true );
if( $udfyld_ean = $order->get_meta('_udfyld_ean') )
echo '<p><strong>'.__('Udfyld EAN', 'woocommerce').': </strong> ' . $udfyld_ean . '</p>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) To display that on Order received, Order view and email notifications you will use:
add_filter( 'woocommerce_get_order_item_totals', 'add_udfyld_ean_row_to_order_totals', 10, 3 );
function add_udfyld_ean_row_to_order_totals( $total_rows, $order, $tax_display ) {;
$new_total_rows = [];
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
if( $order->get_meta('_udfyld_ean') && 'payment_method' === $key ){
$new_total_rows['udfyld_ean'] = array(
'label' => __('Udfyld EAN', 'woocommerce'),
'value' => esc_html( $order->get_meta('_udfyld_ean') ),
);
}
}
return $new_total_rows;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You are using following code to fetch the value of _udfyld_ean :
get_post_meta( $order_id, '_udfyld_ean', true );
But the problem is, you have not defined $order_id anywhere. You will need to pass valid value of order ID to get the expected output.

Change default checkout field value on order creation in Woocommerce 3

In Woocommerce, I am trying to display Email & Phone input fields as a custom field above billing address section.
This is what I did, but failed:
I created some custom fields and displayed them above the billing section. Also, I set the default Email and Phone fields as optional, and removed them. Woo saves order email as a meta into wp_postmenta database table. When I save custom fields, I try to overwrite these meta values with $order->update_meta_data() function, but instead of overwriting it creates a new meta with same meta_key.
function save_extra_checkout_fields( $order, $data ){
if( isset( $data['some_field'] ) ) {
$order->delete_meta_data('_billing_email');
$order->update_meta_data( '_billing_email', sanitize_email( $data['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_create_order', 'save_extra_checkout_fields', 10, 2 );
As you can see even if I try to delete default meta first a duplication occurs.
It is possible to achieve this customization with hooks and filters without hacking the fields in the database, or do you have any idea how to overwrite meta value?
Any help please is welcome.
As billing email is a default field and not custom checkout field, you need to change your code a bit using set_billing_email() WC_Order method instead:
add_action( 'woocommerce_checkout_create_order', 'change_billing_email_checkout_field_value', 10, 2 );
function change_billing_email_checkout_field_value( $order, $data ){
if( isset( $data['some_field'] ) && ! empty( $data['some_field'] ) )
$order->set_billing_email( sanitize_email( $data['some_field'] ) );
}
Code goes in function.php file of your active child theme (or active theme). It should 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();

How to save woocommerce product meta data and then send it in customer email?

After struggling for two days, am now sharing my problem here. I want to save a session value into the meta field of product when product is inserted. currently I am using this code which doesn't work
add_action( 'woocommerce_review_order_after_payment', 'save_product_status_discount', 10, 1 );
function save_product_status_discount($order, $sent_to_admin, $plain_text, $email){
$order_data = $order->get_data();
$item_data = $item_obj->get_data();
$my_post_meta1 = get_post_meta($item_data['order_id'], 'status_discount'.$item_data['order_id'], true);
if(empty ( $my_post_meta1 )){
update_post_meta($item_data['order_id'],'status_discount'.$item_data['order_id'],$_SESSION['status_discount_price']);
}
}
Please help me what should I do. thanks
You could use that, but is not going to work well as you should need to do it for each order item (I think, the correct way is below at the end):
// Save the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'save_product_status_discount', 10, 1 );
function save_product_status_discount( $order_id ) {
$status_discount = get_post_meta($order_id, 'status_discount'.$order_id, true);
if(empty ( $status_discount ) && ! empty( $_SESSION['status_discount_price'] ) )
update_post_meta( $order_id, 'status_discount'.$order_id, $_SESSION['status_discount_price'] );
}
}
// Display the order meta with field value
add_action( 'woocommerce_review_order_after_payment', 'display_product_status_discount', 10, 4 );
function save_product_status_discount($order, $sent_to_admin, $plain_text, $email){
$status_discount = get_post_meta($order_id, 'status_discount'.$order_id, true);
if( ! empty ( $status_discount ) )
echo '<p>'.__( 'Status discount', 'woocommerce' ) . ': ' . $status_discount . '</p>
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The correct way to do it, is to incorporate this "status discount" as item ID meta data, because you can have many items in an order.
So the best option and working code should be simply that compact function:
// Save the order item meta data with custom field value and display it as order items meta data
add_action('woocommerce_add_order_item_meta','save_product_status_discount', 1, 3 );
function save_product_status_discount( $item_id, $values, $cart_item_key ) {
if( empty( $_SESSION['status_discount_price'] ) )
wc_add_order_item_meta( $item_id, 'Status discount', $_SESSION['status_discount_price'], true );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested in WooCommerce 3+ and works.

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

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/

Categories