概要
woocommerce_shipping_$THIS->ID_option
フィルタは、WooCommerceの配送オプションに関連するカスタマイズを行うためのフックです。このフィルタを使用することで、特定の配送メソッドに基づいてオプションや設定を変更することができます。通常、これにより以下のような機能を実装することが多いです。
- 配送手数料の計算ロジックの変更
- 特定の条件下での配送オプションの表示/非表示
- 配送オプションにカスタムメタデータの追加
- 配送方法の説明文の変更
- 配送手段の名称の変更
- お客様の位置情報に基づく配送オプションの調整
構文
add_filter('woocommerce_shipping_$THIS->id_option', 'your_function_name', 10, 2);
パラメータ
$value
– デフォルトのオプション値。$instance
– ウェアハウスや配送メソッドのインスタンス。
戻り値
このフィルタによって変更された新しいオプション値が戻ります。
バージョン情報
- WooCommerceバージョン: 4.0以上
- WordPressバージョン: 5.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_fee', 'custom_flat_rate_fee', 10, 2);
function custom_flat_rate_fee($fee, $instance) {
if ($instance->id === 'flat_rate') {
$fee += 5; // フラットレートに5ドルの手数料を追加
}
return $fee;
}
(情報源: WooCommerce Documentation)
サンプル2: 配送方法の名称を変更する
このサンプルは、特定の配送方法の名称をカスタマイズします。
add_filter('woocommerce_shipping_method_label', 'custom_shipping_method_label', 10, 2);
function custom_shipping_method_label($label, $method) {
if ($method->id === 'free_shipping') {
$label = '無料配送 (期間限定)'; // 無料配送の名称を変更
}
return $label;
}
(情報源: WooCommerce Documentation)
サンプル3: 特定の条件で配送オプションを非表示にする
このサンプルは、特定の条件に基づいて配送オプションを非表示にするためのフィルタです。
add_filter('woocommerce_shipping_methods', 'hide_shipping_methods_based_on_condition');
function hide_shipping_methods_based_on_condition($methods) {
if (/* 条件 */) {
unset($methods['flat_rate']); // フラットレートを非表示にする
}
return $methods;
}
(情報源: WooCommerce Documentation)
サンプル4: 配送オプションにカスタムメタデータを追加する
このサンプルは、配送オプションにカスタムメタデータを追加します。
add_filter('woocommerce_shipping_method_settings', 'add_custom_meta_to_shipping_option', 10, 2);
function add_custom_meta_to_shipping_option($settings, $method) {
if ($method->id === 'local_pickup') {
$settings[] = array(
'id' => 'custom_meta_key',
'title' => 'カスタムメタデータ',
'type' => 'text',
'desc' => 'この配送方法に関連するカスタムメタデータ',
);
}
return $settings;
}
(情報源: WooCommerce Documentation)
サンプル5: 配送オプションの説明文を変更する
このサンプルは、選択した配送オプションの説明文をカスタマイズします。
add_filter('woocommerce_shipping_free_shipping_description', 'custom_free_shipping_description');
function custom_free_shipping_description($description) {
return 'すべての注文で送料無料です!'; // 説明文を変更
}
(情報源: WooCommerce Documentation)