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

概要

woocommerce_calc_shipping_taxフィルタは、WooCommerceプラグイン内で、配送料に対する消費税の計算をカスタマイズするために使用されるフックです。このフィルタを使うことで、特定の条件に基づいて異なる税率を適用したり、税金の計算方式を変更したりすることが可能です。このフィルタは、WooCommerceのカートやチェックアウト時に配送料や税金の計算を行う際によく利用されます。

一般的な用途:

  1. 配送先による税率の変更
  2. 特定の製品カテゴリに基づく税の調整
  3. 配送料の自動計算に対する税金のカスタマイズ
  4. 地域ごとの税政策に対応するための改良
  5. 複数の税率を同時に適用する必要がある場面
  6. 特定のプロモーションに基づく税計算の変更

このフィルタを使用するには、WooCommerce 及び WordPress の最新バージョンが必要です。特に、WooCommerceのバージョンは3.0以上、WordPressのバージョンは4.7以上での使用が推奨されます。

構文

add_filter('woocommerce_calc_shipping_tax', 'custom_calc_shipping_tax', 10, 3);

パラメータ

  • $taxes: 計算された税率の配列
  • $shipping_method: 使用される配送メソッド
  • $order: WooCommerceの注文オブジェクト

戻り値

  • 更新された税率の配列

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

アクション 使用例
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('woocommerce_calc_shipping_tax', 'custom_shipping_tax_by_country', 10, 3);

function custom_shipping_tax_by_country($taxes, $shipping_method, $order) {
    // 配送先が特定の国である場合、異なる税率を適用
    if ($order->get_shipping_country() === 'JP') {
        $taxes[] = 0.10; // 10%の税率を追加
    }
    return $taxes;
}

上記のコードは、配送先が日本の場合に10%の税率を適用します。これにより、日本への配送での税金計算がカスタマイズされます。

引用元: https://www.woocommerce.com/documentation/plugins/woocommerce/

サンプルコード2

add_filter('woocommerce_calc_shipping_tax', 'adjust_tax_for_product_category', 10, 3);

function adjust_tax_for_product_category($taxes, $shipping_method, $order) {
    foreach ($order->get_items() as $item) {
        if (has_term('books', 'product_cat', $item->get_product_id())) {
            // 書籍の場合、税率を0%に設定
            $taxes = [0];
        }
    }
    return $taxes;
}

このコードは、書籍カテゴリの製品がカートに含まれている場合、税率を0%に設定します。

引用元: https://docs.woocommerce.com/document/using-hooks/

サンプルコード3

add_filter('woocommerce_calc_shipping_tax', 'calculate_discounted_tax', 10, 3);

function calculate_discounted_tax($taxes, $shipping_method, $order) {
    if ($order->get_total() > 100) {
        // 注文合計が100ドルを超える場合、税率を0.05減少
        foreach ($taxes as &$tax) {
            $tax -= 0.05;
        }
    }
    return $taxes;
}

このサンプルでは、注文の合計額が100ドルを超える場合に税率を5%引き下げます。

引用元: https://www.wpbeginner.com/

サンプルコード4

add_filter('woocommerce_calc_shipping_tax', 'special_event_tax_adjustment', 10, 3);

function special_event_tax_adjustment($taxes, $shipping_method, $order) {
    // 特定の日(例えば、ホリデーセール)の場合、税率を引き上げ
    if (date('m-d') === '11-29') { // 11月29日
        foreach ($taxes as &$tax) {
            $tax += 0.02; // 2%追加
        }
    }
    return $taxes;
}

このコードは、11月29日(例えば、ブラックフライデー)に2%の税率を追加するものです。

引用元: https://developer.woocommerce.com/

サンプルコード5

add_filter('woocommerce_calc_shipping_tax', 'custom_shipping_tax_based_on_user_role', 10, 3);

function custom_shipping_tax_based_on_user_role($taxes, $shipping_method, $order) {
    if (current_user_can('wholesale_customer')) {
        // 卸売顧客の場合、税率を設定しない
        return [];
    }
    return $taxes;
}

このコードは、卸売顧客に対して税率を設定しないようにしています。

引用元: https://www.tutsmake.com/how-to-change-shipping-tax-in-woocommerce/

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


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