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

概要

woocommerce_rate_percent フィルタは、WooCommerceのカートやチェックアウトプロセスにおける送料や税金などの率をカスタマイズするために使用されます。このフィルタを利用することで、開発者は特定の条件に基づいて税率や送料割合を動的に変更することができます。このフィルタは以下のような機能を実装する際によく使われます。

  1. 商品の種類によって異なる税率を適用する場合
  2. 定期的に変更するプロモーションによる送料割引の設定
  3. 特定の地域における税率の変更
  4. 商品の価格による送料の割引設定
  5. 販売促進のための商品カテゴリーによる特典適用
  6. カスタム条件に合わせて税率を動的に調整する場合

構文

add_filter('woocommerce_rate_percent', 'custom_rate_percent', 10, 2);

パラメータ

  • $rate_percent (float): デフォルトの送料や税率の割合
  • $context (string): フィルタの適用対象(例: ‘shipping’, ‘tax’)

戻り値

  • (float): 修正された送料や税率の割合

対応バージョン

  • WooCommerce: 3.0以上
  • WordPress: 4.5以上

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

アクション 使用例
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: 特定地域への送料割引

このサンプルコードは、特定の地域に対して送料を10%割引にする方法を示しています。

add_filter('woocommerce_rate_percent', 'discount_shipping_rate_for_region', 10, 2);

function discount_shipping_rate_for_region($rate_percent, $context) {
    if (isset(WC()->customer) && 'shipping' === $context) {
        $shipping_country = WC()->customer->get_shipping_country();
        if ($shipping_country === 'JP') { // 日本への配送
            $rate_percent -= 10; // 10%割引
        }
    }
    return $rate_percent;
}

サンプルコード2: 商品カテゴリーによる税率調整

このサンプルコードでは、特定のカテゴリーの商品に対して税率を変更します。

add_filter('woocommerce_rate_percent', 'adjust_tax_rate_based_on_category', 10, 2);

function adjust_tax_rate_based_on_category($rate_percent, $context) {
    if ('tax' === $context) {
        foreach (WC()->cart->get_cart() as $cart_item) {
            if (has_term('premium', 'product_cat', $cart_item['product_id'])) {
                $rate_percent += 5; // プレミアム商品に5%追加
            }
        }
    }
    return $rate_percent;
}

サンプルコード3: セール商品の特別税率

このコードは、セール商品に特別な税率を適用します。

add_filter('woocommerce_rate_percent', 'special_tax_rate_on_sale_items', 10, 2);

function special_tax_rate_on_sale_items($rate_percent, $context) {
    if ('tax' === $context) {
        foreach (WC()->cart->get_cart() as $cart_item) {
            if ($cart_item['data']->is_on_sale()) {
                $rate_percent -= 2; // セール商品に2%割引
            }
        }
    }
    return $rate_percent;
}

サンプルコード4: 高額商品に対する送料割引

このコードは、特定の価格以上の商品に対して送料を無料にします。

add_filter('woocommerce_rate_percent', 'free_shipping_for_high_value_items', 10, 2);

function free_shipping_for_high_value_items($rate_percent, $context) {
    if ('shipping' === $context) {
        $cart_total = WC()->cart->get_subtotal();
        if ($cart_total > 5000) { // 5000円以上の合計
            return 0; // 送料を無料に
        }
    }
    return $rate_percent;
}

サンプルコード5: カスタム条件によるパーセント調整

このサンプルコードでは、カスタム条件(特定のカスタムフィールドがある場合など)に基づいて税率を調整します。

add_filter('woocommerce_rate_percent', 'custom_field_based_tax_rate', 10, 2);

function custom_field_based_tax_rate($rate_percent, $context) {
    if ('tax' === $context) {
        foreach (WC()->cart->get_cart() as $cart_item) {
            $custom_field = get_post_meta($cart_item['product_id'], 'custom_tax_adjustment', true);
            if (!empty($custom_field)) {
                $rate_percent += floatval($custom_field); // カスタムフィールドに基づいて調整
            }
        }
    }
    return $rate_percent;
}

これらのサンプルコードは、異なる条件に基づいて woocommerce_rate_percent フィルタを使用して料金を調整する方法を示しています。各コードスニペットは、WooCommerceのフックを利用して動的な料金設定を行う役割を果たしています。

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


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