dongzhila3786 2018-06-08 18:00
浏览 73
已采纳

在Woocommerce的订单电子邮件通知中显示包含和不包含增值税的订单总额

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条回答 默认 最新

  • duandao7704 2018-06-08 23:15
    关注

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

    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.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器