概要
woocommerce_cart_totals_order_total_html
フィルタは、WooCommerce のカート合計ページにおける注文合計の HTML 形式を変更するために使用されます。このフィルタを使用すると、表記をカスタマイズしたり、追加のデータを表示したりすることができます。代表的な利用シーンとして以下の6つがあります:
- スペシャルオファーやキャンペーン情報の追加
- カスタムスタイルやクラスを追加して見た目を改善
- 税金や送料を明示的に表示するための修正
- 多言語サイトでのカスタム表示
- 特定の条件に基づいた割引の適用
- 追加の情報やメッセージの表示
構文
add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_order_total_html', 10, 1 );
パラメータ
string
$order_total_html : カートの合計値を含む HTML 文字列int
$cart : カートオブジェクト
戻り値
このフィルタは、変更された HTML 文字列を返します。
互換性
- 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: カスタムクラスの追加
このサンプルは、カートの合計欄にカスタムクラスを追加して、CSSでスタイリングできるようにします。
add_filter( 'woocommerce_cart_totals_order_total_html', 'add_custom_class_to_order_total' );
function add_custom_class_to_order_total( $order_total_html ) {
return str_replace( 'class="order-total"', 'class="order-total custom-class"', $order_total_html );
}
サンプル 2: 割引の説明を追加
このサンプルでは、カートの合計に特定の条件に基づいて追加の説明を挿入します。
add_filter( 'woocommerce_cart_totals_order_total_html', 'add_discount_info_to_order_total' );
function add_discount_info_to_order_total( $order_total_html ) {
$discount_info = '<p class="discount-message">10%の割引が適用されています!</p>';
return $order_total_html . $discount_info;
}
サンプル 3: 特別オファーの表示
このサンプルコードでは、特別なオファーがある場合にメッセージを表示します。
add_filter( 'woocommerce_cart_totals_order_total_html', 'display_special_offer_on_order_total' );
function display_special_offer_on_order_total( $order_total_html ) {
$offer_message = '<strong>今だけ!全商品10%オフ!</strong>';
return $order_total_html . $offer_message;
}
サンプル 4: 税金の表示を強調
このサンプルコードでは、税金の部分を強調表示するためにスタイルを追加します。
add_filter( 'woocommerce_cart_totals_order_total_html', 'highlight_tax_in_order_total' );
function highlight_tax_in_order_total( $order_total_html ) {
return str_replace( '税金', '<span style="color:red;">税金</span>', $order_total_html );
}
サンプル 5: 多言語サポートの追加
このサンプルは、翻訳可能なテキストを追加して、多言語サイトのサポートを強化します。
add_filter( 'woocommerce_cart_totals_order_total_html', 'add_translatable_text_to_order_total' );
function add_translatable_text_to_order_total( $order_total_html ) {
$translated_text = __( '合計金額', 'text-domain' );
return $order_total_html . '<p>' . $translated_text . '</p>';
}
それぞれのサンプルコードの具体的な使い方や参考にするための情報は、公式のWooCommerceドキュメントやプラグインの開発者向けのリソースをチェックすると良いでしょう。