【问题标题】:Add a new row in order totals for Woocommerce Thankyou Page and emails在 Woocommerce 感谢页面和电子邮件的订单总数中添加新行
【发布时间】:2018-03-15 09:19:29
【问题描述】:

我对在电子邮件和感谢页面中添加一行的代码有疑问。

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 10, 2 );
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
    $total_rows['recurr_not'] = array(
        'label' => __( 'Rec:', 'woocommerce' ),
        'value' => 'blabla'
    );
    return $total_rows;
}

我有一个功能可以在购物车中添加“Total excl. VAT”行:

<?php 
    global $woocommerce;
    $frais = 1.01; 
    echo '<tr class ="totalht">
    <th>'. __( 'Total HT', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'Total HT', 'woocommerce' ) .' ">'
    . wc_price( ( $woocommerce->cart->cart_contents_total * $frais ) + $woocommerce->cart->shipping_total ) .'<span class="ht-panier">HT</span></td>
    </tr>';
?>

运行良好:https://prnt.sc/irglky

但是当我修改第一个函数时:

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 5, 2 );
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
    global $woocommerce;
    $frais = 1.01;
    $price_excl_vat = wc_price( ( $woocommerce->cart->cart_contents_total * $frais ) + $woocommerce->cart->shipping_total );
    $total_rows['recurr_not'] = array(
        'label' => __( 'Total HT :', 'woocommerce' ),
        'value' => $price_excl_vat 
    );

    return $total_rows;
}

它不起作用on the Thank you page
但它正在工作on email...

有人可以解释一下为什么它可以在电子邮件上运行,但在“感谢页面”中却不行?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce orders


    【解决方案1】:

    更新:因为这个钩子是为了订单数据,但不是购物车数据,你应该试试这个,我在最后一行之前设置了额外的行:

    add_filter( 'woocommerce_get_order_item_totals', 'add_custom_order_totals_row', 30, 3 );
    function add_custom_order_totals_row( $total_rows, $order, $tax_display ) {
        $costs = 1.01;
    
        // Set last total row in a variable and remove it.
        $gran_total = $total_rows['order_total'];
        unset( $total_rows['order_total'] );
    
        // Insert a new row
        $total_rows['recurr_not'] = array(
            'label' => __( 'Total HT :', 'woocommerce' ),
            'value' => wc_price( ( $order->get_total() - $order->get_total_tax() ) * $costs  ),
        );
    
        // Set back last total row
        $total_rows['order_total'] = $gran_total;
    
        return $total_rows;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。 经过测试且有效


    对于购物车,您应该使用这个 (因为不再需要 global $woocommerce;

    <?php 
        $costs = 1.01; 
    
        echo '<tr class ="totalht">
        <th>'. __( 'Total HT', 'woocommerce' ) .'</th>
        <td data-title=" '. __( 'Total HT', 'woocommerce' ) .' ">'
        . wc_price( ( WC()->cart->cart_contents_total * $costs ) + WC()->cart->shipping_total ) .'<span class="ht-panier">HT</span></td>
        </tr>';
    ?>
    

    【讨论】:

    • @ArthurM 好的,这只是一个小错字,现在再试一次。
    • @ArthurM 刚刚进行了另一个更新,以使最后一个“Total”行之前出现“Total HT”……试试看。它有效。
    猜你喜欢
    • 2021-10-16
    • 2021-05-02
    • 2020-06-25
    • 2016-01-04
    • 2021-04-11
    • 2016-10-04
    • 2013-06-13
    • 2022-01-10
    • 2018-04-15
    相关资源
    最近更新 更多