概要
woocommerce_get_formatted_order_total
フィルタは、WooCommerceで注文の合計金額をフォーマットする際に使用されます。このフィルタは、テーマやプラグインが注文の合計金額を表示する方法をカスタマイズできる柔軟な手段を提供します。以下は、このフィルタがよく使用される機能の例です。
- 価格表示の単位を変更する。
- 特定の条件に基づいて値引きを適用する。
- 言語や通貨に基づいて金額のフォーマットを調整する。
- 請求書やレシート用の金額フォーマットをカスタマイズする。
- 特定の販売者や顧客に応じて価格を表示する。
- 背景にあるロジックに基づいて追加料金を加算する。
構文
add_filter('woocommerce_get_formatted_order_total', 'custom_formatted_order_total', 10, 2);
パラメータ
string $formatted_total
– フォーマットされた合計金額WC_Order $order
– 対象の注文オブジェクト
戻り値
string
– カスタマイズ後のフォーマットされた合計金額
使用可能なバージョン
- WooCommerce: 3.0.0 以上
- WordPress: 4.0 以上
サンプルコード
サンプルコード1: 通貨記号の追加
add_filter('woocommerce_get_formatted_order_total', 'add_currency_symbol_to_order_total', 10, 2);
function add_currency_symbol_to_order_total($formatted_total, $order) {
return '¥' . $formatted_total;
}
このコードは、注文合計金額の前に通貨記号(日本円)を追加します。
サンプルコード2: 割引を適用する
add_filter('woocommerce_get_formatted_order_total', 'apply_discount_to_order_total', 10, 2);
function apply_discount_to_order_total($formatted_total, $order) {
$discount = 500; // 固定割引額
$order_total = $order->get_total() - $discount;
return wc_price($order_total);
}
このコードは、固定割引額を適用した後の注文合計金額を表示します。
サンプルコード3: 特定の条件で文字列を追加
add_filter('woocommerce_get_formatted_order_total', 'append_message_to_order_total', 10, 2);
function append_message_to_order_total($formatted_total, $order) {
if ($order->get_total() > 10000) {
return $formatted_total . ' (大口割引適用)';
}
return $formatted_total;
}
このコードは、注文合計が1万円を超える場合にメッセージを追加します。
サンプルコード4: カスタムフォーマット
add_filter('woocommerce_get_formatted_order_total', 'custom_format_order_total', 10, 2);
function custom_format_order_total($formatted_total, $order) {
return '合計金額: ' . $formatted_total;
}
このコードは、合計金額の前にカスタムテキストを追加して表示します。
サンプルコード5: 小数点以下のスタイルを変更
add_filter('woocommerce_get_formatted_order_total', 'change_decimal_style_order_total', 10, 2);
function change_decimal_style_order_total($formatted_total, $order) {
return number_format((float) $order->get_total(), 2, ',', '.') . '円';
}
このコードは、小数点の表示スタイルを変更し、結果をフォーマットします。
この関数のアクションでの使用可能性
アクション | 使用可能 |
---|---|
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 |