问题
I would like to edit the contents of the Woocommerce invoice that a customers gets after ordering. I think I have to edit this file located at wp-content/plugins/woocommerce/templates/emails/email-order-details.php
Using the following code:
<tr>
<th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ?
'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['label'] ); ?></th>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : '';
?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
Now it shows the VAT details on the same rule as the total price, but I would like it to be on a separate rule as follows:
Totaal: €50,00
BTW: €8,68
Does anyone know how to do this?
回答1:
If you want to have this change on all email notifications, you will use the following code, that will display the gran total (without displayed taxes) and the taxes in a separated new row:
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 ){
// Only on emails notifications
if( ! is_wc_endpoint_url() ) {
// Change: Display only the gran total amount
$total_rows['order_total']['value'] = strip_tags( wc_price( $order->get_total() ) );
// Create a new row for total tax
$new_row = array( 'order_tax_total' => array(
'label' => __('BTW:','woocommerce'),
'value' => strip_tags( wc_price( $order->get_total_tax() ) )
) );
// Add the new created to existing rows
$total_rows += $new_row;
}
return $total_rows;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If you want to target only the "Customer invoice" email notification, instead you will need to make changes on the template emails/email-order-details.php
overriding it through your theme.
Please read first the documentation: Template structure & Overriding templates via a theme
Once you have copied emails/email-order-details.php
template file to your theme's folder, open/edit it.
On line 64 after:
$totals = $order->get_order_item_totals();
Add the following:
// Only Customer invoice email notification
if ( $email->id === 'customer_invoice' ):
// Change: Display only the gran total amount
$totals['order_total']['value'] = strip_tags( wc_price( $order->get_total() ) );
// Create a new row for total tax
$new_row = array( 'order_tax_total' => array(
'label' => __('BTW:','woocommerce'),
'value' => strip_tags( wc_price( $order->get_total_tax() ) )
) );
// Add the new created to existing rows
$totals += $new_row;
endif;
Tested and works too…
来源:https://stackoverflow.com/questions/52929010/add-the-tax-total-as-a-separated-row-on-woocommerce-email-notifications