概要
woocommerce_cart_taxes_total
フィルタは、WooCommerceにおけるカートの税金合計を変更するために使用されます。開発者はこのフックを利用することで、カート内の商品に適用される税金の合計額をカスタマイズできます。主に以下の機能の実装に役立ちます。
- 税金の計算方法のカスタマイズ
- 特定の条件に基づく割引の適用
- 税率の変更
- 表示される税金合計のフォーマット変更
- 税金別のレポート生成
- マルチカレンシー対応の税金計算
構文
add_filter('woocommerce_cart_taxes_total', 'custom_cart_taxes_total', 10, 2);
パラメータ
$taxes_total
(float): カート内の税金合計$cart
(WC_Cart): WooCommerceカートオブジェクト
戻り値
- (float)変更された税金合計
使用可能なバージョン
- WooCommerce: 3.0以降
- WordPress: 4.0以降
サンプルコード
サンプルコード1: 固定税額の追加
このコードは、カートの税金合計に固定額の税金を追加します。
add_filter('woocommerce_cart_taxes_total', 'add_fixed_tax', 10, 2);
function add_fixed_tax($taxes_total, $cart) {
$fixed_tax = 5.00; // 固定税額
return $taxes_total + $fixed_tax;
}
サンプルコード2: 割引税率の計算
このコードは、コンディションによって異なる税率を適用します。
add_filter('woocommerce_cart_taxes_total', 'conditional_tax_rate', 10, 2);
function conditional_tax_rate($taxes_total, $cart) {
if ($cart->subtotal > 100) {
return $taxes_total * 0.80; // 20%割引税率
}
return $taxes_total;
}
サンプルコード3: 税金フォーマット変更
このコードは、税金合計の表示フォーマットを変更します。
add_filter('woocommerce_cart_taxes_total', 'format_taxes_total', 10, 2);
function format_taxes_total($taxes_total, $cart) {
return number_format($taxes_total, 2) . ' ¥'; // 日本円形式で表示
}
サンプルコード4: 特定カテゴリー商品に税金を適用しない
このコードは、特定のカテゴリーの商品には税金を適用しない設定をします。
add_filter('woocommerce_cart_taxes_total', 'exclude_tax_for_specific_category', 10, 2);
function exclude_tax_for_specific_category($taxes_total, $cart) {
foreach ($cart->get_cart() as $cart_item) {
if (has_term('no-tax', 'product_cat', $cart_item['product_id'])) {
return 0; // 税金をゼロにする
}
}
return $taxes_total;
}
サンプルコード5: 税金に関するメッセージを追加
このコードは、税金合計に関するメッセージをカートに追加します。
add_filter('woocommerce_cart_taxes_total', 'add_message_to_taxes_total', 10, 2);
function add_message_to_taxes_total($taxes_total, $cart) {
if ($taxes_total > 0) {
wc_add_notice(__('Taxes are included in your cart total.', 'woocommerce'), 'notice');
}
return $taxes_total;
}
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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 |