簡體   English   中英

根據 WooCommerce 訂單中的用戶角色顯示自定義稅收總計行

[英]Display custom tax total row based on user roles in WooCommerce orders

我正在 WooCommerce 中建立一個網上商店,我需要在訂單電子郵件中顯示正確的稅金。 我有 2 個不同的用戶角色:“私人”和“企業”

以下代碼進行稅收計算並將其添加到新行。 但是,對於“商業”用戶角色,我需要 output 為 0DKK (因為他們支付的價格不含稅)

// 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.25 );
    $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' => __('Moms udgør:','woocommerce'),
        'value' => $total_tax_amount
    ) );

    $total_rows['order_total']['value'] = wc_price( $gran_total );

    return $total_rows + $tax_row;
}

任何幫助將不勝感激。

對於“業務”用戶角色,以下內容會將您的自定義總稅行更改為零:

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 ){
    $user  = $order->get_user(); // Gets WP_User Object from order
    $total = $order->get_total(); // Gets order grand total. incl. taxes

    // Clean total row displayed taxes
    $total_rows['order_total']['value'] = is_wc_endpoint_url() ? wc_price($total) : strip_tags(wc_price($total));

    // Get total tax formatted from a defined tax rate based on user roles
    $total_tax = in_array('business', $user->roles) ? wc_price(0) : wc_price($total - ($total / 1.25));

    // Insert total tax row
    $total_rows['order_tax_total'] = array(
        'label' => __('Moms udgør:','woocommerce'),
        'value' => is_wc_endpoint_url() ? $total_tax : strip_tags($total_tax),
    );

    return $total_rows;
}

它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM