概要
woocommerce_matched_tax_rates
フィルタは、WooCommerceで使用される税率をカスタマイズするためのフックです。このフィルタは、ショッピングカートやチェックアウトプロセスにおいて、商品の税金計算を調整したり、特定の条件に基づいて異なる税率を適用したりする際に役立ちます。特に以下のような機能を実装する際によく使用されます。
- 地域ごとの税率のカスタマイズ
- 商品タイプやカテゴリに基づく異なる税率の適用
- 特定の顧客に対する税率の特別処理
- 期間限定の税率割引の適用
- 重量や大きさに基づく税率の調整
- カスタム条件に基づいた税率の計算
構文
apply_filters( 'woocommerce_matched_tax_rates', $tax_rates, $customer_id, $address );
パラメータ
$tax_rates
: 差し替える税率の配列$customer_id
: 顧客のID$address
: 顧客の住所情報を含む配列
戻り値
このフィルタは、税率の配列を返します。
使用可能なバージョン
- 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( 'woocommerce_matched_tax_rates', 'custom_tax_rates_based_on_location', 10, 3 );
function custom_tax_rates_based_on_location( $tax_rates, $customer_id, $address ) {
if ( $address['country'] == 'JP' ) {
// 日本向けの税率を適用
$tax_rates = array( 'JP_TAX_RATE' );
}
return $tax_rates;
}
このコードは、日本の顧客に対して特定の税率を適用するためのサンプルです。
サンプルコード 2: 商品タイプに基づく税率の適用
add_filter( 'woocommerce_matched_tax_rates', 'custom_tax_rates_based_on_product_type', 10, 3 );
function custom_tax_rates_based_on_product_type( $tax_rates, $customer_id, $address ) {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
foreach ( $cart_items as $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product( $product_id );
if ( $product->get_type() == 'virtual' ) {
// バーチャル商品には異なる税率を適用
$tax_rates = array( 'VIRTUAL_PRODUCT_TAX_RATE' );
}
}
return $tax_rates;
}
このコードは、バーチャル商品に異なる税率を適用するためのサンプルです。
サンプルコード 3: 顧客の特別処理
add_filter( 'woocommerce_matched_tax_rates', 'apply_special_tax_rate_for_loyal_customers', 10, 3 );
function apply_special_tax_rate_for_loyal_customers( $tax_rates, $customer_id, $address ) {
if ( is_user_logged_in() && user_has_loyalty_status( $customer_id ) ) {
// ロイヤルティプログラムに参加している顧客に特別な税率を適用
$tax_rates = array( 'LOYAL_CUSTOMER_TAX_RATE' );
}
return $tax_rates;
}
function user_has_loyalty_status( $user_id ) {
// ロイヤルティステータスを確認するロジックをここに記述
return true; // 仮に常に真を返す
}
このコードは、ロイヤルティプログラムに参加している顧客に対して特別な税率を適用するためのサンプルです。
サンプルコード 4: 期間限定の税率割引
add_filter( 'woocommerce_matched_tax_rates', 'apply_temporal_discount_tax_rate', 10, 3 );
function apply_temporal_discount_tax_rate( $tax_rates, $customer_id, $address ) {
$current_date = current_time( 'Y-m-d' );
$start_date = '2023-01-01';
$end_date = '2023-02-01';
if ( $current_date >= $start_date && $current_date <= $end_date ) {
// 期間中は特別な税率を適用
$tax_rates = array( 'TEMPORAL_DISCOUNT_TAX_RATE' );
}
return $tax_rates;
}
このコードは、特定の期間内に特別な税率割引を適用するためのサンプルです。
サンプルコード 5: 商品のサイズに基づく税率の調整
add_filter( 'woocommerce_matched_tax_rates', 'adjust_tax_rate_based_on_product_size', 10, 3 );
function adjust_tax_rate_based_on_product_size( $tax_rates, $customer_id, $address ) {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
foreach ( $cart_items as $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product( $product_id );
if ( $product->get_attribute( 'size' ) == 'large' ) {
// 大サイズの商品には異なる税率を適用
$tax_rates = array( 'LARGE_PRODUCT_TAX_RATE' );
}
}
return $tax_rates;
}
このコードは、大サイズの商品に異なる税率を適用するためのサンプルです。