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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀