概要
woocommerce_payment_gateway_get_new_payment_method_option_html_label
フィルタは、WooCommerceにおける新しい支払い方法のオプションのHTMLラベルをカスタマイズするために使用されます。このフックを利用することで、オンラインショップの支払いページでユーザーに提供される支払いオプションの表示を変更できます。例えば、独自のラベルを追加したり、既存のラベルを翻訳したりすることが可能です。
このフィルタは、以下のような機能を実装する際によく使用されます。
- 支払い方法の名称のカスタマイズ
- 新しい支払いオプションの追加
- ローカライズ(翻訳)対応
- 支払いオプションの条件付き表示
- ユーザーインターフェースの改善
- ブランド固有のラベルや説明の追加
構文
add_filter('woocommerce_payment_gateway_get_new_payment_method_option_html_label', 'custom_label_function', 10, 2);
パラメータ
- $label (string): 変更前のラベルテキスト
- $gateway (WC_Payment_Gateway): 現在の支払いゲートウェイのインスタンス
戻り値
変更されたラベルテキスト(string)
使用可能なバージョン
- WooCommerce: 3.0 以降
- WordPress: 4.0 以降
サンプルコード
サンプルコード 1
add_filter('woocommerce_payment_gateway_get_new_payment_method_option_html_label', 'custom_payment_label', 10, 2);
function custom_payment_label($label, $gateway) {
return __('Custom Payment Method', 'your-text-domain');
}
このサンプルコードでは、新しい支払いオプションのラベルを「Custom Payment Method」に変更します。翻訳対応も行われています。
サンプルコード 2
add_filter('woocommerce_payment_gateway_get_new_payment_method_option_html_label', 'conditional_payment_label', 10, 2);
function conditional_payment_label($label, $gateway) {
if ('cod' === $gateway->id) {
return __('Cash on Delivery', 'your-text-domain');
}
return $label;
}
このサンプルコードは、支払い方法が「代金引換」の場合に特定のラベルを設定します。それ以外の支払い方法では、元のラベルを保持します。
サンプルコード 3
add_filter('woocommerce_payment_gateway_get_new_payment_method_option_html_label', 'brand_specific_label', 10, 2);
function brand_specific_label($label, $gateway) {
return $label . ' – Powered by My Brand';
}
このコードは、各支払いオプションのラベルに「Powered by My Brand」を追加しています。
サンプルコード 4
add_filter('woocommerce_payment_gateway_get_new_payment_method_option_html_label', 'dynamic_label_based_on_user', 10, 2);
function dynamic_label_based_on_user($label, $gateway) {
if (is_user_logged_in()) {
return __('Welcome Back! Choose your payment method:', 'your-text-domain');
}
return $label;
}
このサンプルコードでは、ユーザーがログインしている場合に異なるラベルを表示します。
サンプルコード 5
add_filter('woocommerce_payment_gateway_get_new_payment_method_option_html_label', 'custom_label_with_icon', 10, 2);
function custom_label_with_icon($label, $gateway) {
return '<span class="payment-icon"></span>' . $label;
}
このコードは、支払いオプションのラベルの前にアイコンを追加しています。
この関数のアクションでの使用可能性
アクション名 | 使用可能性 |
---|---|
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 |