プラグインWooCommerceのwc_round_tax_totalフィルタの使用方法・解説

概要

wc_round_tax_totalフィルタは、WooCommerceの税額計算結果を調整するために使用されるフックです。このフィルタを通じて、税額の表示や計算をカスタマイズすることが可能です。以下は、wc_round_tax_totalフィルタが使用される典型的なシナリオの例です。

  1. 税額の丸め方法を変更する。
  2. 1つ以上の税率に基づいて総税額を計算する。
  3. 特定の地域や条件に基づいて異なる税額を適用する。
  4. 通貨の変更にともなう税額の再計算を行う。
  5. 税額の表示フォーマットをカスタマイズする。
  6. 顧客グループに応じた税率を適用する。

構文

add_filter('wc_round_tax_total', 'function_name', 10, 2);

パラメータ

  • float $total: 現在の税額の合計
  • object $order: 影響を受けるWooCommerceの注文オブジェクト

戻り値

  • float: 調整された税額の合計

使用可能なプラグインのバージョン

  • WooCommerce: 3.0以上
  • WordPress: 4.0以上

この関数のアクションでの使用可能性

アクション 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード1

add_filter('wc_round_tax_total', 'custom_round_tax_for_special_cases', 10, 2);
function custom_round_tax_for_special_cases($total, $order) {
    if ($order->get_total() > 100) {
        return round($total, 0); // 大きな注文の場合は小数点以下を丸める
    }
    return $total;
}

説明: 注文の合計が100を超える場合、税額を小数点以下で丸めます。

サンプルコード2

add_filter('wc_round_tax_total', 'adjust_tax_for_woocommerce_tax_classes', 10, 2);
function adjust_tax_for_woocommerce_tax_classes($total, $order) {
    foreach ($order->get_items() as $item) {
        if ($item->get_tax_class() === 'reduced-rate') {
            return $total * 0.9; // 特定の税クラスに対して90%に減額
        }
    }
    return $total;
}

説明: 特定の税クラス (‘reduced-rate’) に対して税額を90%に減額します。

サンプルコード3

add_filter('wc_round_tax_total', 'custom_currency_tax_rounding', 10, 2);
function custom_currency_tax_rounding($total, $order) {
    if (get_woocommerce_currency() === 'JPY') {
        return round($total); // 日本円の場合は丸める
    }
    return $total;
}

説明: 通貨が日本円の場合、税額を丸めます。

サンプルコード4

add_filter('wc_round_tax_total', 'custom_group_based_tax', 10, 2);
function custom_group_based_tax($total, $order) {
    if (current_user_can('wholesale_customer')) {
        return $total * 0.85; // 卸売顧客には15%割引を提供
    }
    return $total;
}

説明: 卸売顧客に対して税額を15%割引します。

サンプルコード5

add_filter('wc_round_tax_total', 'display_tax_inclusive_price', 10, 2);
function display_tax_inclusive_price($total, $order) {
    return ceil($total); // 税額を切り上げて表示
}

説明: 税額を切り上げて表示します。

この関数について質問する


上の計算式の答えを入力してください