Reputation: 45
I'd like to remove the auto-generated order number from both the "Order on-hold" and "New order" emails generated by WooCommerce.
I'm using a third-party plugin to assign custom order numbers after an order has been placed, so it's important that the new order number I assign can still be used in future emails. I don't want the customer (or admin) to see the original order number until it has been changed.
Any help would be greatly appreciated!
Upvotes: 2
Views: 3887
Reputation: 4100
REMOVE ORDER NUMBER VIA CSS
If you don't want to override the email templates you can use the woocommerce_email_styles
hook to add a simple CSS rule that hides the order number in WooCommerce email.
This hook is activated just after the template is loaded: /woocommerce/emails/email-styles.php
To hide the order number only for the "Order on-hold" and "New Order" templates you can use the second $ email argument of the woocommerce_email_styles hook to check the id. Find a list here: Targeting specific email with the email id in Woocommerce.
// hides the order number in the email templates
add_filter( 'woocommerce_email_styles', 'add_woocommerce_email_styles', 10, 2 );
function add_woocommerce_email_styles( $css, $email ) {
// define the ids of the emails for which you want to add CSS rules
$email_ids = array(
'new_order',
'customer_on_hold_order'
);
// adds CSS rules to these emails only
if ( in_array( $email->id, $email_ids ) ) {
$css .= 'div[id^="body_content_inner"] > h2 { display: none; }';
}
return $css;
}
The code has been tested and works. Add it to your active theme's functions.php.
ADD THE NEW ORDER NUMBER
Based on this answer you can also add the new order number, like so:
// adds the new order number before the order table in the completed order email
add_action( 'woocommerce_email_before_order_table', 'add_content_before_order_table', 99, 4 );
function add_content_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
// define the ids of the emails for which you want to add CSS rules
$email_ids = array(
'new_order',
'customer_on_hold_order'
);
// adds CSS rules to these emails only
if ( in_array( $email->id, $email_ids ) ) {
echo '<p>New order number</p>'; // do not use the <h2> tag otherwise it will be hidden
}
}
The code has been tested and works. Add it to your active theme's functions.php.
Upvotes: 0
Reputation: 253773
Updated (only for woocommerce 3.3+ specific template)
You will need to override a Woocommerce email template via your child theme as explained on the below linked official documentation:
Template structure & Overriding templates via a theme
The template to copy and override is woocommerce/templates/emails/email-order-details.php
In this template (copied to your theme as explained) you will need to change this entire block:
<h2>
<?php
if ( $sent_to_admin ) {
$before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
$after = '</a>';
} else {
$before = '';
$after = '';
}
/* translators: %s: Order ID. */
echo wp_kses_post( $before . sprintf( __( 'Order #%s', 'woocommerce' ) . $after . ' (<time datetime="%s">%s</time>)', $order->get_order_number(), $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ) );
?>
</h2>
to:
<?php
// Targetting specific email notificatoins
$email_ids = array('new_order', 'customer_on_hold_order');
$date = sprintf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) );
// Displaying order number except for "New Order" and "Customer On Hold Order" notifications
if( ! in_array($email->id, $email_ids) ){
$order_number = sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() );
$date = '('.$date.')';
} else {
$date = __('Order date:', 'woocommerce') . ' ' . $date;
$order_number = '';
}
if ( $sent_to_admin ) {
$before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
$after = '</a> ';
} else {
$before = '';
$after = ' ';
}
?>
<h2><?php echo $before . $order_number . $after . $date; ?></h2>
This will remove the Order number on "New Order" and "Customer On Hold Order" email notifications. You will get:
1) New order (admin):
2) Customer On-Hold Order:
Now you will also need in WooCommerce > Settings > Emails to remove ({order_number})
from "New Order" subject and save…
You are done…
Upvotes: 2