【问题标题】:Add tax below the total in WooCommerce order details在 WooCommerce 订单详细信息中添加低于总额的税款
【发布时间】:2019-08-31 16:18:44
【问题描述】:

在 WooCommerce 中,我正在尝试在订单详细信息中添加低于总金额的税款。我想在订单总数中添加一行。但是下面的代码不起作用:

//Show tax in order details
add_action( 'woocommerce_checkout_create_order', 'func_new_total', 20, 1 );
function func_new_total( $order ) {
    // Get order total
    $total = $order->get_total();
    //Calculate tax
    $tax = $total * 0.2;

    //Add tax on new line below total
    $new_total = $total . '<br>' . 'herav Mva kr' . $tax;

    $order->set_total($new_total);
}

这个我也试过了,还是不行:

add_action('woocommerce_checkout_update_order_meta', function( $order_id, $posted ) {
    $order = wc_get_order( $order_id );

    $total_price = $order->get_total();
    $tax = $total_price * 0.2;
    $new_total = $total_price . '<br>' . 'herav Mva kr' . $tax;

    update_post_meta( $order_id, 'total', 'hallooo' );  
} , 10, 2);

【问题讨论】:

    标签: php wordpress woocommerce orders tax


    【解决方案1】:

    您可以使用以下命令,在订单总额下方的单独行中显示总税额(对于订单和电子邮件通知)

    // Add total taxes as a separated line before order total on orders and emails
    add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
    function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
        // Display only the gran total amount
        $gran_total = wc_price( $order->get_total() );
        $total_rows['order_total']['value'] = is_wc_endpoint_url() ? $gran_total : strip_tags( $gran_total );
    
        $total_tax_amount = wc_price( $order->get_total_tax() );
        $total_tax_amount = is_wc_endpoint_url() ? $total_tax_amount : strip_tags( $total_tax_amount );
    
        // Create a new row for total tax
        $tax_row = array( 'order_tax_total' => array(
            'label' => __('Herav MVA:','woocommerce'),
            'value' => $total_tax_amount
        ) );
    
        $total_rows['order_total']['value'] = $gran_total;
    
        return $total_rows + $tax_row;
    }
    

    或使用您的自定义总税 20% 计算:

    // Add total taxes as a separated line before order total on orders and emails
    add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
    function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
        // Display only the gran total amount
        $gran_total = (float) $order->get_total();
        $total_rows['order_total']['value'] = is_wc_endpoint_url() ? $total_html : strip_tags( $total_html );
    
        // Custom tax calculation (for 20% tax rate)
        $total_tax_amount = wc_price(  $gran_total - $gran_total / 1.2 );
        $total_tax_amount = is_wc_endpoint_url() ? $total_tax_amount : strip_tags( $total_tax_amount );
    
        // Create a new row for total tax
        $tax_row = array( 'order_tax_total' => array(
            'label' => __('Herav MVA:','woocommerce'),
            'value' => $total_tax_amount
        ) );
    
        $total_rows['order_total']['value'] = wc_price( $gran_total );
    
        return $total_rows + $tax_row;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • LoicTheAztec 再次救援! :D 非常感谢,效果很好:)
    • 这对我来说不起作用。我正在使用 woocommerce 3.6.5。你能帮忙吗?
    猜你喜欢
    • 2015-01-28
    • 2020-09-28
    • 2019-02-28
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    相关资源
    最近更新 更多