概要
woocommerce_order_formatted_line_subtotal
フィルタは、WooCommerceにおいて注文の各行の小計金額をフォーマットするために使用されるフックです。このフィルタを使用すると、配列の値やフォーマットの変更、カスタマイズを行うことができ、以下のような機能を実装する際によく使われます。
- 小計金額のフォーマット変更
- ユーザー地域に基づく通貨の変更
- 税金の表示形式のカスタマイズ
- 割引の適用情報の表示
- 特定の製品に対するカスタムメッセージの追加
- フロントエンド表示を変更するための条件付き表示の導入
構文
add_filter( 'woocommerce_order_formatted_line_subtotal', 'your_function_name', 10, 3 );
パラメータ
$formatted_subtotal
(string): フォーマットされた小計金額$subtotal
(float): 小計金額$order
(WC_Order): 現在の注文オブジェクト
戻り値
- フィルタされたフォーマットされた小計金額 (string)
互換性
- WooCommerce バージョン: 3.0 以降
- 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: 小計金額のカスタムフォーマット
add_filter( 'woocommerce_order_formatted_line_subtotal', 'custom_subtotal_format', 10, 2 );
function custom_subtotal_format( $formatted_subtotal, $subtotal ) {
return '小計: ' . wc_price( $subtotal );
}
このサンプルコードでは、フォーマットされた小計金額の前に「小計: 」という文字列を追加しています。
サンプル2: 特定の製品に対するカスタムメッセージ
add_filter( 'woocommerce_order_formatted_line_subtotal', 'add_custom_message_to_subtotal', 10, 3 );
function add_custom_message_to_subtotal( $formatted_subtotal, $subtotal, $order ) {
if ( /* 条件: 例えば特定の製品ID */ ) {
$formatted_subtotal .= ' (特別割引適用)';
}
return $formatted_subtotal;
}
このコードは、特定の製品に対して小計の後にカスタムメッセージを追加しています。
サンプル3: 通貨記号の変更
add_filter( 'woocommerce_order_formatted_line_subtotal', 'change_currency_symbol', 10, 2 );
function change_currency_symbol( $formatted_subtotal, $subtotal ) {
return str_replace( '$', '¥', $formatted_subtotal ); // 通貨記号をドルから円に変更
}
このサンプルでは、表示される通貨記号をドルから円に変更しています。
サンプル4: 税金額を小計に追加
add_filter( 'woocommerce_order_formatted_line_subtotal', 'add_tax_to_subtotal', 10, 3 );
function add_tax_to_subtotal( $formatted_subtotal, $subtotal, $order ) {
$tax = $order->get_tax_totals();
if ( !empty( $tax ) ) {
$formatted_subtotal .= ' (税金: ' . wc_price( $tax ) . ')';
}
return $formatted_subtotal;
}
このコードでは、小計金額の後に税金額を表示しています。
サンプル5: 小計金額の円周表示
add_filter( 'woocommerce_order_formatted_line_subtotal', 'circular_format_subtotal', 10, 2 );
function circular_format_subtotal( $formatted_subtotal, $subtotal ) {
return '🔵 ' . $formatted_subtotal . ' 🔵'; // 小計の周りに円を表示
}
このサンプルでは、小計金額の周りに円の絵文字を追加しています。
引用元として具体的なURLは示していませんが、この情報はWooCommerceの開発者向けドキュメントを基にしています。