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

概要

woocommerce_shipping_method_supports フィルタは、WooCommerceにおける配送メソッドの機能を変更または拡張する際に使用されます。このフィルタを使うことで、特定の配送方法がサポートする機能(例:無料配送、トラッキング番号、配送日時の選択など)を追加または変更することが可能です。以下はこのフィルタがよく使われる機能の例です。

  1. 無料配送の有効化
  2. トラッキング番号のサポート
  3. 配送日時の選択機能の追加
  4. 配送地域の制限
  5. 複数の配送オプションの表示
  6. 配送手数料の調整

構文

add_filter('woocommerce_shipping_method_supports', 'your_function_name', 10, 3);

パラメータ

  • $supports:サポート機能の配列。
  • $method:対象の配送メソッドのインスタンス。
  • $instance_id:配送メソッドのインスタンスID。

戻り値

  • 修正された $supports 配列(サポートする機能のリスト)。

使用可能なバージョン

  • WooCommerceのバージョン:2.0以降
  • WordPressのバージョン:3.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_shipping_method_supports', 'add_tracking_support', 10, 2);
function add_tracking_support($supports, $method) {
    if ('flat_rate' === $method->id) {
        $supports[] = 'tracking';
    }
    return $supports;
}

サンプルコード 2

このコードは、特定のメソッドが無料配送をサポートするように変更します。

add_filter('woocommerce_shipping_method_supports', 'enable_free_shipping', 10, 2);
function enable_free_shipping($supports, $method) {
    if ('free_shipping' === $method->id) {
        $supports[] = 'free_shipping';
    }
    return $supports;
}

サンプルコード 3

この例では、配送日時の選択機能を特定の配送メソッドに追加します。

add_filter('woocommerce_shipping_method_supports', 'add_delivery_date_support', 10, 2);
function add_delivery_date_support($supports, $method) {
    if ('local_pickup' === $method->id) {
        $supports[] = 'delivery_date';
    }
    return $supports;
}

サンプルコード 4

このコードは、特定の地域に対する配送方法の制限を設定します。

add_filter('woocommerce_shipping_method_supports', 'restrict_shipping_region', 10, 2);
function restrict_shipping_region($supports, $method) {
    if ('flat_rate' === $method->id) {
        $supports[] = 'specific_region';
    }
    return $supports;
}

サンプルコード 5

この例では、複数の配送オプションを表示する機能を特定の配送メソッドに追加します。

add_filter('woocommerce_shipping_method_supports', 'enable_multiple_options', 10, 2);
function enable_multiple_options($supports, $method) {
    if ('express' === $method->id) {
        $supports[] = 'multiple_options';
    }
    return $supports;
}

引用元のURLはそれぞれありませんが、これらはWooCommerceの公式ドキュメントや一般的なWordPressのプラクティスに基づいています。

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


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