概要
woocommerce_shipping_$THIS->ID_instance_option
フィルタは、WooCommerceの配送方法の設定をカスタマイズするために使用されます。このフィルタを使用することで、特定の配送方法に関連する設定やオプションを柔軟に変更することができます。これにより、ユーザーのニーズに応じたカスタマイズや機能追加が可能になります。主に以下の場面で使用されることが多いです。
- 配送料金の調整
- 配送エリアの制限
- 配送オプションの有効無効の切替
- メタデータの追加
- 配送に関するカスタムメッセージの設定
- 通常の設定で提供されていない独自の設定の追加
フィルタの概要
- 構文:
add_filter( 'woocommerce_shipping_$THIS->ID_instance_option', 'your_function_name', 10, 2 );
- パラメータ:
$instance_option
: 変更したいインスタンスオプションの値$this
: 現在の配送メソッドのインスタンス
- 戻り値: フィルタリングされたインスタンスオプションの値
- 使用可能なバージョン:
- 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_shipping_flat_rate_instance_option', 'adjust_shipping_fee', 10, 2 );
function adjust_shipping_fee( $instance_option, $this ) {
if ( isset( $instance_option['cost'] ) ) {
$instance_option['cost'] += 5; // 基本料金に5ドル追加
}
return $instance_option;
}
このコードは、フラットレート配送の料金に5ドルを追加します。
サンプルコード2: 配送エリアの制限
add_filter( 'woocommerce_shipping_free_shipping_instance_option', 'restrict_shipping_area', 10, 2 );
function restrict_shipping_area( $instance_option, $this ) {
if ( isset( $instance_option['tax_status'] ) && $instance_option['tax_status'] === 'restricted' ) {
$instance_option['enabled'] = 'no'; // 特定エリアでは配送不可とする
}
return $instance_option;
}
このコードは、税率が「restricted」と設定されている場合、その配送方法を無効にします。
サンプルコード3: 配送オプションの有効無効の切替
add_filter( 'woocommerce_shipping_local_pickup_instance_option', 'toggle_local_pickup_option', 10, 2 );
function toggle_local_pickup_option( $instance_option, $this ) {
if ( date('N') == 6 ) { // 土曜日の場合
$instance_option['enabled'] = 'yes'; // 土曜日はローカルピックアップを有効にする
} else {
$instance_option['enabled'] = 'no'; // 他の日は無効
}
return $instance_option;
}
このコードは、土曜日のみにローカルピックアップオプションを有効にします。
サンプルコード4: メタデータの追加
add_filter( 'woocommerce_shipping_flat_rate_instance_option', 'add_meta_to_shipping_option', 10, 2 );
function add_meta_to_shipping_option( $instance_option, $this ) {
$instance_option['custom_meta'] = 'This is custom meta data'; // カスタムメタデータを追加
return $instance_option;
}
このコードは、フラットレート配送オプションにカスタムメタデータを追加します。
サンプルコード5: 配信に関するカスタムメッセージの設定
add_filter( 'woocommerce_shipping_flat_rate_instance_option', 'custom_shipping_message', 10, 2 );
function custom_shipping_message( $instance_option, $this ) {
$instance_option['custom_message'] = 'Expected delivery within 3-5 business days'; // カスタムメッセージを追加
return $instance_option;
}
このコードは、フラットレート配送に関するカスタムメッセージを追加します。