Display order total with and without VAT in Woocom

2019-08-11 04:56发布

问题:

I'm trying to edit the order email to add non-VAT price !

As I've not already found a way to do it the way I wanted using email-order-details.php and as I'm afraid of broking things even in a child theme .php document, I've try to do it with my own snippets, working trough function.php :

    add_action( 'woocommerce_email_after_order_table', 'add_order_email_instructions', 10, 2 );

    function add_order_email_instructions( $order, $sent_to_admin ) {
       $order_data = $order->get_data();
       $order_total = $order_data['cart_tax'];
       $order_total_tax = $order_data['total_tax'];

       if ( ! $sent_to_admin ) {
         echo '
        <!-- TABLEAU à ajouter à la suite -->
        <!-- rowspan=n dans <td …> pour prendre n colonnes et colspan=n dans <td …> pour prendre n ligne -->

    <h2>Détails de votre bon de commande </h2>
    <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: Helvetica, Roboto, Arial, sans-serif;" border="1">
        <thead>
        <!-- Il y a 6 colonnes et autant de ligne que de <tr></tr> -->
        </thead>
      <tbody>
        <!-- Ca c est la première ligne : Les <th> sont des titres -->
            <tr>
                <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Garantie Constructeur</th>
                <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">24 mois</td>
                <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Incluse</td>
            </tr>
            <tr>
                <th class="td" scope="col" colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Livraison</th>
                <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Incluse</td>
            </tr>
            <tr>
                <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Délai de fabrication</th>
                <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">3 mois max.</td>
                <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"></td>
            </tr>
            <tr>
                <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Délai de livraison</th>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">3 semaines max.</td>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"></td>
        </tr>
       <tr>
            <th class="td" scope="col" colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Sous-total H.T.</th>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">???</td>
         </tr>
       <tr> 
    <!-- Chaque ligne de code dans un <tr> remplit chaqune des colonnes --> 
        <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">TVA</th>
        <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">20 %</td>
        <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">???*0.2</td> <!-- Sur tout les produits et options.-->
            </tr>
            <tr>
                <th class="td" scope="col" colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Total T.T.C.</th>
                <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">???*1.2</td>
            </tr>
            <!-- -->
        </tbody>
        <tfoot>
            <!-- -->
        </tfoot>
    </table>';
        }
    }

This code try to recreate the table one can see in order mail generated by WooCommerce, with my own customs cells : In fact I need raws with some custom text, then a raw with the sub total without VAT, then a raw with the VAT alone, then a raw with the total with VAT include.

In my specific exemple, I do not get why replacing ??? in my code by <?php echo wp_kses_post( $order_total_tax ); ?> doesn't work.

Any help is appreciated.

回答1:

Try the following using the WC_Abstract_Order getter methods on the WC_Order object:

  • get_total_tax()
  • get_total()

To get the Total excluding taxes, it's just a calculation:

 $get_total_excl_taxes = $order->get_total() - $order->get_total_tax();

So in your code:

add_action( 'woocommerce_email_after_order_table', 'add_order_email_instructions', 20, 2 );
function add_order_email_instructions( $order, $sent_to_admin ) {

   if ( ! $sent_to_admin ) {
     echo '
    <!-- TABLEAU à ajouter à la suite -->
    <!-- rowspan=n dans <td …> pour prendre n colonnes et colspan=n dans <td …> pour prendre n ligne -->

<h2>Détails de votre bon de commande </h2>
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: Helvetica, Roboto, Arial, sans-serif;" border="1">
    <thead>
    <!-- Il y a 6 colonnes et autant de ligne que de <tr></tr> -->
    </thead>
  <tbody>
    <!-- Ca c est la première ligne : Les <th> sont des titres -->
        <tr>
            <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Garantie Constructeur</th>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">24 mois</td>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Incluse</td>
        </tr>
        <tr>
            <th class="td" scope="col" colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Livraison</th>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Incluse</td>
        </tr>
        <tr>
            <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Délai de fabrication</th>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">3 mois max.</td>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"></td>
        </tr>
        <tr>
            <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Délai de livraison</th>
        <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">3 semaines max.</td>
        <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"></td>
    </tr>
   <tr>
        <th class="td" scope="col" colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Sous-total H.T.</th>
        <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">'.wc_price($order->get_total() - $order->get_total_tax()).'</td>
     </tr>
   <tr>
<!-- Chaque ligne de code dans un <tr> remplit chaqune des colonnes -->
    <th class="td" scope="col" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;">TVA</th>
    <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">20 %</td>
    <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">'.wc_price($order->get_total_tax()).'</td> <!-- Sur tout les produits et options.-->
        </tr>
        <tr>
            <th class="td" scope="col" colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>;">Total T.T.C.</th>
            <td class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;">'.wc_price($order->get_total()).'</td>
        </tr>
        <!-- -->
    </tbody>
    <tfoot>
        <!-- -->
    </tfoot>
</table><br>';
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.