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

概要

woocommerce_order_is_vat_exempt フィルタは、WooCommerce で注文が VAT(付加価値税)から免除されるかどうかを判定するために使用されます。このフィルタを利用することで、特定の条件に基づいて VAT 免除の適用をカスタマイズすることができます。よく使われるケースには次のようなものがあります。

  1. 特定のユーザーグループや顧客に対する VAT 免除の適用
  2. 国や地域に基づく VAT 免除の設定
  3. 商品カテゴリーの条件に基づく VAT 免除
  4. 特定のメンバーシップやプロモーションプログラムの適用
  5. カスタムフィールドに基づく税の免除
  6. ショッピングカートの内容に基づく税金計算の調整

構文

apply_filters( 'woocommerce_order_is_vat_exempt', $is_exempt, $order );

パラメータ

  • $is_exempt (bool): 現在の税免除ステータス(デフォルトは false)。
  • $order (WC_Order): 評価対象の注文オブジェクト。

戻り値

  • (bool): 免除すべきかどうかを示す真偽値。

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

  • 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_order_is_vat_exempt', 'custom_vat_exempt_for_specific_user', 10, 2 );

function custom_vat_exempt_for_specific_user( $is_exempt, $order ) {
    if ( $order->get_user_id() === 1 ) { 
        return true; // User ID 1 is VAT exempt
    }
    return $is_exempt;
}

このサンプルコードは、特定のユーザー(IDが1のユーザー)が VAT 免除であるかどうかを判定し、そのユーザーの注文に対して VAT 免除を適用します。

サンプルコード 2

add_filter( 'woocommerce_order_is_vat_exempt', 'exempt_orders_from_us', 10, 2 );

function exempt_orders_from_us( $is_exempt, $order ) {
    $shipping_country = $order->get_shipping_country();
    if ( $shipping_country === 'US' ) { 
        return true; // US orders are VAT exempt
    }
    return $is_exempt;
}

このコードは、注文の発送先がアメリカ合衆国の場合、この注文に VAT 免除を適用します。

サンプルコード 3

add_filter( 'woocommerce_order_is_vat_exempt', 'exempt_product_category', 10, 2 );

function exempt_product_category( $is_exempt, $order ) {
    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id();
        $terms = get_the_terms( $product_id, 'product_cat' );
        if ( $terms ) {
            foreach ( $terms as $term ) {
                if ( $term->slug === 'tax-exempt-category' ) { 
                    return true; // Product in tax-exempt category
                }
            }
        }
    }
    return $is_exempt;
}

このコードは、注文内に特定のカテゴリー(ここでは ‘tax-exempt-category’)に属する商品がある場合に、その注文に VAT 免除を適用します。

サンプルコード 4

add_filter( 'woocommerce_order_is_vat_exempt', 'custom_exempt_based_on_cart_contents', 10, 2 );

function custom_exempt_based_on_cart_contents( $is_exempt, $order ) {
    if ( $order->get_cart_contents_count() > 5 ) { 
        return true; // More than 5 items in cart grants VAT exemption
    }
    return $is_exempt;
}

このサンプルコードは、カート内の商品数が5点以上であれば、その注文に VAT 免除を適用します。

サンプルコード 5

add_filter( 'woocommerce_order_is_vat_exempt', 'custom_exempt_for_membership', 10, 2 );

function custom_exempt_for_membership( $is_exempt, $order ) {
    if ( is_user_logged_in() && user_can( $order->get_user_id(), 'premium_member' ) ) { 
        return true; // Premium members are VAT exempt
    }
    return $is_exempt;
}

このコードは、ログインしているユーザーが「premium_member」ロールを持っている場合、そのユーザーの注文に VAT 免除を適用します。

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


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