WooCommerce provides a robust eCommerce solution for WordPress users, but sometimes, the default email templates don’t fully meet business requirements. In this blog, we’ll explore how to customize WooCommerce’s order email template by adding a new tax field and modifying the shipping details dynamically.

Why Customize WooCommerce Email Templates?

By default, WooCommerce sends order confirmation emails with standard fields like order subtotal, shipping cost, and taxes. However, businesses may need additional fields or specific tax calculations that aren’t included out of the box. In this case, we wanted to:

  1. Add a Standard Tax field that calculates tax on each item based on a specific formula.
  2. Update the price of each order product in the invoice by removing the tax.
  3. Show a new subtotal price, which is the sum of all item prices without taxes.
  4. Add a VAT on Shipping field when applicable.
  5. Change the shipping field title to “Shipping (Including VAT)” when the shipping fee is added in the Order details.

Implementing the Custom WooCommerce Email Modification

To modify the WooCommerce email template, we use the woocommerce_get_order_item_totals filter, which allows us to customize the order totals displayed in the email.

Here’s the complete solution that implements all the required modifications:

add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ) {

// Ensure this only runs for emails
if ( ! did_action( 'woocommerce_email_order_details' ) ) {
return $total_rows;
}

$subtotal = $order->get_subtotal();
$subtotal_without_tax = $subtotal / (float) 'Country Tax Dividened here';
$total_tax = $subtotal - $subtotal_without_tax;

$new_total_rows = array();
$shipping_fee = 0;

foreach ( $total_rows as $key => $value ) {
if ( 'cart_subtotal' === $key ) {
$new_total_rows[$key] = $value;

$new_total_rows['vat'] = array(
'label' => __( 'VAT', 'woocommerce' ),
'value' => wc_price( $total_tax ),
);
} elseif ( 'shipping' === $key ) {
// Extract the numerical value from the shipping field
$shipping_fee = floatval(preg_replace('/[^0-9.]/', '', $value['value'])); // Remove currency symbols

if ( $shipping_fee === 'whatever you added in Woo' ) {
// Change the title and add the VAT on Shipping field
$new_total_rows[$key] = array(
'label' => __( 'Shipping (Including VAT)', 'woocommerce' ),
'value' => wc_price( $shipping_fee ),
);

$new_total_rows['vat_on_shipping'] = array(
'label' => __( 'VAT on Shipping', 'woocommerce' ),
'value' => wc_price('Country VAT'), // VAT on shipping value
);
} else {
// If shipping fee is not added in the order details, add the shipping field as is
$new_total_rows[$key] = $value;
}
} else {
// Keep other total rows as they are
$new_total_rows[$key] = $value;
}
}

return $new_total_rows;
}

Explanation of the Code

  • Ensuring the Code Runs Only for Emails:
    • The did_action(‘woocommerce_email_order_details’) function ensures that the changes are only applied to WooCommerce email notifications.
  • Adding the VAT Field:
    • We calculate the VAT by dividing the subtotal by ‘Country specific Tax Divider’ and then subtracting the result from the original subtotal.
    • The calculated VAT is added as a new row in the order summary.
  • Updating the Shipping Details:
    • The shipping cost is extracted from the order details and converted into a float value.
    • If the shipping cost is exactly ‘Whatever you added in WooCommerce’, the label is changed to Shipping (Including VAT) and an additional field VAT on Shipping is added with a fixed value of Shippind Fee added in WooCommerce.

Why This Matters

Customizing WooCommerce email templates is essential for businesses that require precise tax calculations and enhanced invoice details. By implementing this solution, we:

  • Provide more clarity on tax values.
  • Improve the readability of order confirmation emails.
  • Ensure compliance with local tax regulations.

Conclusion

With WooCommerce’s flexible filters, we can easily modify email templates to meet specific business needs. Whether you need to add new fields, update pricing calculations, or customize the shipping details, the approach outlined in this guide provides a solid foundation for enhancing WooCommerce order emails.

Do you have any other customizations you’d like to implement? Let us know in the comments!

Leave a Reply

Your email address will not be published. Required fields are marked *