I am using WooCommerce plugin for an e-commerce business and when I create an order or change its state there always an email being sent to informing the current state of the order. For some current reasons there is no information on the price of the products and the WooCommerce email templates having the price, subtotal, and total data within the email order details. What I need is to modify those templates to remove the price, subtotal, and total data and also to change the addresses headings.
I browsed the woocommerce plugin folder to find where the email templates are and I googled and found a detracted solution that lies in removing the hook that generating the order data, found at WC_Emails class constructor, then add it again and wire it to a custom function that generates the structure desired. The solution works partially for me as if I created an order the first time the order is at on-hold state and an email is sent and looks as needed, however, when I change the state of the order to, for example, processing, or any subsequent order states another email is sent now with two order details tables the one generated by my custom function and below it is the one generated by WooCommerce as shown by the image below.
// add the action
add_action( 'woocommerce_email_order_details', 'remove_order_details', 10, 4);
add_action( 'woocommerce_email_order_details','action_woocommerce_email_order_details', 10, 4);
// define the woocommerce_email_order_details callback
function action_woocommerce_email_order_details($order, $sent_to_admin, $plain_text, $email)
{
$text_align = is_rtl() ? 'right' : 'left';
?>
<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>
<div style="margin-bottom: 40px;">
<table class="td" cellspacing="0" cellpadding="6"
style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
<thead>
<tr>
<th class="td" scope="col"
style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e('Product', 'woocommerce'); ?></th>
<th class="td" scope="col"
style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e('Quantity', 'woocommerce'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($order->get_items() as $item_id => $item) { ?>
<tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>">
<td class="td"
style="text-align:<?php echo esc_attr($text_align); ?>; vertical-align: middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;">
<?php
// Product name.
echo wp_kses_post(apply_filters('woocommerce_order_item_name', $item->get_name(), $item, false));
// allow other plugins to add additional product information here.
do_action('woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text);
// allow other plugins to add additional product information here.
do_action('woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text);
?>
</td>
<td class="td"
style="text-align:<?php echo esc_attr($text_align); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php echo wp_kses_post(apply_filters('woocommerce_email_order_item_quantity', $item->get_quantity(), $item)); ?>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<?php
$meta_data = $order->get_meta('_custom_px_src');
if ($meta_data) {
?>
<tr>
<th class="td" scope="row" colspan="2"
style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e(is_rtl() ? 'وصفة طبية' : 'Prescription:'); ?></th>
<td class="td">
<img src="<?php echo $meta_data['value']; ?>" alt="Prescription image" height="42" width="42">
<a href="<?php echo $meta_data['value']; ?>" target="_blank"></a>
</td>
</tr>
<?php
}
if ($order->get_customer_note()) {
?>
<tr>
<th class="td" scope="row" colspan="2"
style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e('Note:', 'woocommerce'); ?></th>
<td class="td"
style="text-align:<?php echo esc_attr($text_align); ?>;"><?php echo wp_kses_post(wptexturize($order->get_customer_note())); ?></td>
</tr>
<?php
}
?>
</tfoot>
</table>
</div>
<?php
}
function remove_order_details()
{
$mailer = WC()->mailer(); // get the instance of the WC_Emails class
remove_action('woocommerce_email_order_details', array($mailer, 'order_details'));
}
What I expected is a solution that pertain only the order details table generated by my custom function
I found the problem that causes the order details table duplication within the email template and it was adding the woocommerce_email_order_details removal action with a priority equal to the doing action as showing below
add_action( 'woocommerce_email_order_details', 'remove_order_details', 10, 4);
add_action( 'woocommerce_email_order_details','action_woocommerce_email_order_details', 10, 4);
However, what I should do is prioritize the removal action with a higher priority instead
add_action( 'woocommerce_email_order_details', 'remove_order_details', 1, 4);
add_action( 'woocommerce_email_order_details','action_woocommerce_email_order_details', 10, 4);