プラグインWooCommerceのwoocommerce_shipping_$THIS->ID_instance_settings_valuesフィルタの使用方法・解説

概要

woocommerce_shipping_$THIS->ID_instance_settings_valuesは、WooCommerceの特定の配送方法のインスタンス設定を変更するためのフィルターフックです。このフィルターフックは、配送料金の設定やオプションをカスタマイズしたり、管理画面での設定を追加する際に非常に便利です。主に以下の機能に利用されることが多いです。

  1. 配送料金の条件に基づくカスタムロジックの追加
  2. 管理画面での設定フィールドの拡張
  3. 特定の商品の配送制限の実装
  4. ストア全体の設定に基づく配送ルールの適用
  5. 顧客グループごとに異なる配送オプションを設定
  6. サードパーティの配送サービスとの統合

構文

add_filter('woocommerce_shipping_$THIS->id_instance_settings_values', 'custom_function', 10, 2);

パラメータ

  • $settings (配列): 元のインスタンス設定。
  • $this (オブジェクト): 現在の配送メソッドのインスタンス。

戻り値

  • (配列): 変更されたインスタンス設定。

使用可能なWooCommerceのバージョン

  • WooCommerce 2.6以上

使用可能なWordPressのバージョン

  • 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_shipping_flat_rate_instance_settings_values', function($settings, $instance) {
    $settings['custom_option'] = array(
        'title' => __('Custom Option', 'text-domain'),
        'type'  => 'checkbox',
        'default' => 'yes',
    );
    return $settings;
});

説明: フラットレート配送メソッドにカスタムオプションを追加します。

サンプルコード2

add_filter('woocommerce_shipping_free_shipping_instance_settings_values', function($settings, $instance) {
    $settings['minimum_order_amount'] = array(
        'title' => __('Minimum Order Amount', 'text-domain'),
        'type'  => 'number',
        'default' => '50',
    );
    return $settings;
});

説明: 無料配送のインスタンス設定に最小注文金額フィールドを追加します。

サンプルコード3

add_filter('woocommerce_shipping_flat_rate_settings', function($settings) {
    $settings['additional_fee'] = array(
        'title' => __('Additional Fee', 'text-domain'),
        'type'  => 'number',
        'default' => '0',
    );
    return $settings;
});

説明: フラットレート配送の設定に追加料金の項目を追加します。

サンプルコード4

add_filter('woocommerce_shipping_local_pickup_instance_settings_values', function($settings, $instance) {
    $settings['pickup_hours'] = array(
        'title' => __('Pickup Hours', 'text-domain'),
        'type'  => 'text',
        'default' => '9 AM - 5 PM',
    );
    return $settings;
});

説明: ローカルピックアップのインスタンス設定にピックアップ時間を追加します。

サンプルコード5

add_filter('woocommerce_shipping_zones_instance_settings_values', function($settings, $instance) {
    $settings['zone_description'] = array(
        'title' => __('Zone Description', 'text-domain'),
        'type'  => 'textarea',
        'default' => '',
    );
    return $settings;
});

説明: 配送ゾーンのインスタンス設定にゾーンの説明を追加します。

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


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