【问题标题】:Insert a custom total excluding products cost in WooCommerce order emails在 WooCommerce 订单电子邮件中插入不包括产品成本的自定义总计
【发布时间】:2021-08-14 12:42:40
【问题描述】:

我正在使用以下函数将自定义总计添加到购物车和结帐页面(取自 this answer 到我的问题 Insert a custom total excluding products cost row on cart and checkout totals in WooCommerce

function display_custom_total() {
    // Get (sub)total
    $subtotal = WC()->cart->subtotal;
    $total = WC()->cart->total;
    
    // Calculate
    $total_to_pay = $total - $subtotal;
    
    // The Output
    echo ' <tr class="cart-total-to-pay">
        <th>' . __( 'Total (to pay)', 'woocommerce' ) . '</th>
        <td data-title="total-to-pay">' . wc_price( $total_to_pay ) . '</td>
    </tr>';
}
add_action( 'woocommerce_cart_totals_after_order_total', 'display_custom_total', 20 );
add_action( 'woocommerce_review_order_after_order_total', 'display_custom_total', 20 );

如何将其添加到电子邮件中的订单总计表中?

我可以使用与钩子 woocommerce_email_after_order_table 相同的函数将其添加到电子邮件模板中,但将其添加到订单表下方而不是作为额外的行。

我知道我可以在我的子主题(特别是 email-order-details.php)中编辑电子邮件模板文件,但我不知道如何编辑它以将计算添加到该表格下方的新表格行中:

<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>
</tr>

我还需要将其添加到“我的帐户”>“查看订单”页面中(如果它可以是同一功能的一部分)。

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    您可以使用woocommerce_get_order_item_totals 过滤钩子,它允许您向现有表添加新行。

    新行将被添加到:

    • 电子邮件
    • 订单已收到(感谢页面)
    • 我的账户 -> 查看订单
    function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
        // Get (sub)total
        $subtotal = $order->get_subtotal();
        $total = $order->get_total();
        
        // Calculate
        $total_to_pay = $total - $subtotal;
        
        // Add new row
        $total_rows['total_to_pay']['label'] = __( 'Total to pay', 'woocommerce' );
        $total_rows['total_to_pay']['value'] = wc_price( $total_to_pay );
    
        return $total_rows;
    }
    add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );
    

    相关:Insert a custom total excluding products cost row on cart and checkout totals in WooCommerce

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2016-03-03
      • 2021-01-29
      相关资源
      最近更新 更多