概要
woocommerce_product_single_add_to_cart_text
フィルタは、WooCommerceプラグインにおいて、単一の商品ページでの「カートに追加」ボタンのテキストを変更するために使用されます。これは、商品の特性やマーケティング戦略に従って、ボタンのテキストをより魅力的にするためによく使われます。
以下は、このフィルタが実装される際によく見られる機能の例です:
- 「カートに追加」を「今すぐ購入」に変更
- 期間限定セール商品でのテキスト変更
- 特定のカスタム製品タイプに対するボタン名の独自定義
- ユーザーのロール(役割)に基づくテキストの変更
- 在庫切れ商品のボタンを「再入荷通知を受け取る」に変更
- サブスクリプション商品特有のボタンラベル
構文
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text');
パラメータ
$text
(string): 現在のボタンテキスト
戻り値
- (string): 新しいボタンテキスト
使用可能なプラグインWooCommerceのバージョン
- WooCommerce 3.0以上
使用可能なWordPressのバージョン
- 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: ボタンテキストの変更
function custom_add_to_cart_text($text) {
return '今すぐ購入';
}
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text');
このサンプルコードでは、カートに追加ボタンのテキストを「今すぐ購入」に変更しています。
サンプルコード2: 在庫切れ商品のテキスト変更
function change_add_to_cart_text($text, $product) {
if (!$product->is_in_stock()) {
return '再入荷通知を受け取る';
}
return $text;
}
add_filter('woocommerce_product_single_add_to_cart_text', 'change_add_to_cart_text', 10, 2);
このサンプルコードは、商品が在庫切れの場合にボタンのテキストを「再入荷通知を受け取る」に変更します。
サンプルコード3: カスタム製品タイプ向けのテキスト変更
function custom_product_type_text($text, $product) {
if ($product->get_type() === 'custom_type') {
return 'この商品を試す';
}
return $text;
}
add_filter('woocommerce_product_single_add_to_cart_text', 'custom_product_type_text', 10, 2);
このサンプルでは、カスタム製品タイプに対して特定のテキストを設定しています。
サンプルコード4: ユーザーの役割に基づくテキストの変更
function role_based_add_to_cart_text($text) {
if (current_user_can('editor')) {
return 'エディタ専用 - 購入開始';
}
return $text;
}
add_filter('woocommerce_product_single_add_to_cart_text', 'role_based_add_to_cart_text');
このサンプルコードでは、エディタユーザーに対して特定のテキストを表示します。
サンプルコード5: プロモーション用テキストの追加
function promotional_add_to_cart_text($text) {
return $text . ' - 今なら20%オフ!';
}
add_filter('woocommerce_product_single_add_to_cart_text', 'promotional_add_to_cart_text');
このサンプルでは、通常のボタンテキストの後にプロモーション情報を追加しています。