概要
woocommerce_time_format
フィルタは、WooCommerceにおける時間のフォーマットをカスタマイズするために使用されるフックです。このフィルタを利用することにより、販売促進キャンペーンやイベントの日時表示を特定の形式でカスタマイズすることが可能になります。主に次のような機能実装時に利用されることがあります。
- 商品のセール開始・終了時間の表示形式変更
- 注文履歴ページでの取引日時のカスタマイズ
- イベントやセミナーの日時をカスタムフォーマットで表示
- アカウントページでの購買履歴の日時表示形式変更
- プロモーションバナーに表示するカウントダウンタイマーのフォーマット調整
- クーポンの有効期限表示の形式を変更
構文
add_filter('woocommerce_time_format', 'my_custom_time_format');
パラメータ
$time_format
(string): WooCommerceがデフォルトで使用する時間フォーマット。
戻り値
- (string): フィルタを適用した後のカスタマイズされた時間フォーマット。
使用可能なWooCommerceバージョン
- WooCommerce 3.0.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 |
サンプルコード
-
デフォルトの時間フォーマットを変更する
add_filter('woocommerce_time_format', 'custom_time_format_function'); function custom_time_format_function($time_format) { return 'H:i:s'; // 24時間形式に変更 }
このサンプルは、WooCommerce内の時間表示を24時間形式に変更します。これによって、ユーザーがより直感的に時間を理解できるようになります。
-
カスタムフォーマットを適用する
add_filter('woocommerce_time_format', 'apply_custom_time_format'); function apply_custom_time_format($time_format) { return 'l, jS F Y, g:i A'; // 詳細な日時表現 }
ここでは、時間フォーマットを詳細な表現に変更し、曜日、日、月、年、12時間形式に調整しています。
-
特定の条件で時間フォーマットを変更する
add_filter('woocommerce_time_format', 'conditional_time_format'); function conditional_time_format($time_format) { if (is_product()) { return 'd/m/Y H:i'; // 商品ページでは異なる形式 } return $time_format; // その他ではデフォルト }
商品ページでのみ異なる形式に変更し、他のページではデフォルトの時間フォーマットを保持しています。
-
ビジネスロジックに基づいてフォーマットを変更
add_filter('woocommerce_time_format', 'business_logic_time_format'); function business_logic_time_format($time_format) { $current_hour = date('H'); return ($current_hour < 12) ? 'g:i A' : 'H:i'; // 午前と午後でフォーマットを変更 }
現在の時刻に基づいて、午前と午後で異なる時間フォーマットを使用しています。
-
特別なイベント用のカスタムフォーマット
add_filter('woocommerce_time_format', 'event_time_format'); function event_time_format($time_format) { return 'F j, Y - g:i A'; // イベント情報を分かりやすく表示 }
ここでは、特別なイベントの日時表示に適したフォーマットに変更しています。
引用元のページ:
– WordPress Codex
– WooCommerce Documentation
– Stack Overflow