概要
woocommerce_gateway_method_title
フィルタは、WooCommerce の決済ゲートウェイで表示されるメソッドのタイトルを変更するために使用されます。このフィルタは主に、支払いプロセスのカスタマイズを行う際に役立ちます。以下のような機能を実装する際に頻繁に利用されます。
- 支払いタイトルのカスタマイズ
- 複数の支払いオプションのタイトルの翻訳
- 条件に応じた動的な支払いメソッドタイトルの変更
- プラグインによる新しい支払いメソッドの追加
- 顧客の地理的位置に基づいた支払いメソッドタイトルの調整
- ビジュアルスタイルに合わせた支払いメソッド名の変更
構文
add_filter( 'woocommerce_gateway_method_title', 'custom_gateway_method_title', 10, 2 );
パラメータ
$title
(string): 現在の支払いメソッドのタイトル。$payment_method
(WC_Payment_Gateway): 使用される支払いメソッドのオブジェクト。
戻り値
- (string): 変更後の支払いメソッドのタイトル。
WooCommerce および WordPress バージョン
- WooCommerce バージョン: 4.0.0 以上
- WordPress バージョン: 5.0.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_gateway_method_title', 'change_credit_card_title', 10, 2 );
function change_credit_card_title( $title, $payment_method ) {
if ( 'credit_card' === $payment_method->id ) {
return 'お好きなカードでお支払い';
}
return $title;
}
引用元: https://docs.woocommerce.com/document/advanced-filtering-with-hooks/
サンプルコード2: 支払いメソッド名の動的な変更
特定の条件に基づいてタイトルを変更する例です。
add_filter( 'woocommerce_gateway_method_title', 'dynamic_gateway_title', 10, 2 );
function dynamic_gateway_title( $title, $payment_method ) {
if ( isset( $_GET['special_offer'] ) && 'paypal' === $payment_method->id ) {
return '特別オファー用 PayPal';
}
return $title;
}
引用元: https://developer.woocommerce.com/
サンプルコード3: 地域ごとのタイトル設定
顧客の地域に応じてタイトルを変更する例です。
add_filter( 'woocommerce_gateway_method_title', 'region_based_gateway_title', 10, 2 );
function region_based_gateway_title( $title, $payment_method ) {
$customer_country = WC()->customer->get_shipping_country();
if ( 'JP' === $customer_country && 'bank_transfer' === $payment_method->id ) {
return '日本の銀行振込';
}
return $title;
}
引用元: https://woocommerce.com/document/conditional-logic-woocommerce/
サンプルコード4: プラグインによるタイトルの変更
独自の過去のプラグインによってタイトルを変える例です。
add_filter( 'woocommerce_gateway_method_title', 'plugin_custom_title', 10, 2 );
function plugin_custom_title( $title, $payment_method ) {
if ( 'stripe' === $payment_method->id ) {
return 'Stripe(クレジットカード決済)';
}
return $title;
}
引用元: https://www.wpbeginner.com/
サンプルコード5: ビジュアルスタイルのタイトル変更
スタイルに基づいてタイトルを変える例です。
add_filter( 'woocommerce_gateway_method_title', 'styled_gateway_title', 10, 2 );
function styled_gateway_title( $title, $payment_method ) {
return '<span style="color: green;">' . $title . '</span>';
}
引用元: https://www.smashingmagazine.com/
以上が woocommerce_gateway_method_title
フィルタの使用方法の概要とサンプルコードです。各サンプルは異なる用途に対応しており、特定の要件に沿ってタイトルを変更する方法を示しています。