概要
woocommerce_shipping_method_title
は、WooCommerceの配送方法のタイトルをフィルタリングするためのフックです。このフックを使用することで、出荷方法の名称をカスタマイズしたり、特定の条件に基づいて動的に変更したりすることができます。このフィルタは、主に次のような機能を実装する際によく使用されます:
- 出荷方法の名前を複数の言語に変更する(国際化)。
- プロモーションや特別なイベントに基づいて出荷方法のタイトルを変更する。
- 顧客の選択肢に応じて出荷方法を動的に表示する。
- 出荷方法の内容に基づいてカスタムメッセージを追加する。
- テストやレビュー用の仮の出荷方法名を設定する。
- 特定の条件によって出荷方法をそれぞれ異なるタイトルに変更する。
構文
add_filter('woocommerce_shipping_method_title', 'custom_shipping_method_title', 10, 2);
パラメータ
string $title
:出荷方法の元のタイトル。WC_Shipping_Method $method
:出荷方法のオブジェクト。
戻り値
- 変更された出荷方法のタイトル(文字列)。
使用可能なバージョン
- 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_method_title', 'change_shipping_method_title', 10, 2);
function change_shipping_method_title($title, $method) {
if ($method->id === 'flat_rate') {
$title = '特別料金';
}
return $title;
}
このサンプルコードでは、”flat_rate” (定額配送) の出荷方法のタイトルを”特別料金”に変更します。
サンプルコード2:国による出荷方法タイトルの変更
add_filter('woocommerce_shipping_method_title', 'country_based_shipping_title', 10, 2);
function country_based_shipping_title($title, $method) {
if (WC()->customer->get_shipping_country() === 'JP') {
$title .= '(日本向け)';
}
return $title;
}
このサンプルコードは、日本向けの出荷方法の場合、元のタイトルに”(日本向け)”を追加します。
サンプルコード3:特定のプロモーション中のタイトル変更
add_filter('woocommerce_shipping_method_title', 'promo_shipping_title', 10, 2);
function promo_shipping_title($title, $method) {
if (is_product_on_sale()) {
$title .= '(特別プロモーション中)';
}
return $title;
}
このサンプルコードは、セール中の製品の配送方法のタイトルに”(特別プロモーション中)”を追加します。
サンプルコード4:ユーザーの選択肢によるタイトルの変更
add_filter('woocommerce_shipping_method_title', 'dynamic_shipping_title', 10, 2);
function dynamic_shipping_title($title, $method) {
if (isset($_POST['delivery_option']) && $_POST['delivery_option'] == 'express') {
$title = 'エクスプレス配達';
}
return $title;
}
このサンプルコードでは、ユーザーがエクスプレス配達を選択した場合、配送方法のタイトルを”エクスプレス配達”に変更します。
サンプルコード5:出荷方法のタイトルにカスタムメッセージを追加
add_filter('woocommerce_shipping_method_title', 'custom_message_shipping_title', 10, 2);
function custom_message_shipping_title($title, $method) {
return $title . ' - お急ぎの方はこちら!';
}
このサンプルコードは、すべての出荷方法のタイトルに” – お急ぎの方はこちら!”というカスタムメッセージを追加します。