プラグインWooCommerceのwoocommerce_cart_totals_after_order_totalアクションの使用方法・解説

概要

woocommerce_cart_totals_after_order_total は、WooCommerceのカートの合計金額の表示後に追加の内容を出力するためのフックです。このアクションは、カートやチェックアウトページでカスタム情報や追加料金を表示する際に非常に便利です。具体的には、以下のような機能を実装する際に使用されます:

  1. カスタム手数料の表示
  2. 割引情報の追加
  3. クレジットクーポンの適用内容の詳細表示
  4. 追加の税金情報の明確化
  5. 顧客へのメッセージや注意事項の表示
  6. 特別オファーやプロモーションのトリガー

構文

add_action('woocommerce_cart_totals_after_order_total', 'your_custom_function');

パラメータ

  • このアクションにはパラメータはありません。

戻り値

  • このアクション自体は値を返しませんが、主にHTMLを出力するために使用されます。

使用可能なプラグインWooCommerceのバージョン

  • WooCommerceのバージョン: 5.0以降

ワードプレスのバージョン

  • WordPressのバージョン: 5.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_action('woocommerce_cart_totals_after_order_total', 'display_custom_message');

function display_custom_message() {
    echo '<tr class="custom-message"><th>特別オファー:</th><td>今すぐ購入で10%割引!</td></tr>';
}

サンプルコード2:追加手数料の表示

このコードは、カート合計の後に追加の手数料を表示します。

add_action('woocommerce_cart_totals_after_order_total', 'add_custom_fee');

function add_custom_fee() {
    $fee = 5.00; // 固定手数料
    echo '<tr class="fee"><th>追加手数料:</th><td>' . wc_price($fee) . '</td></tr>';
}

サンプルコード3:税金の明細を表示

このコードでは、カート合計の後に税金の明細を表示します。

add_action('woocommerce_cart_totals_after_order_total', 'show_tax_details');

function show_tax_details() {
    $tax = WC()->cart->get_taxes_total();
    echo '<tr class="tax-details"><th>税金:</th><td>' . wc_price($tax) . '</td></tr>';
}

サンプルコード4:割引コードの追加情報

このコードは、カート合計の後に割引コードの詳細を表示します。

add_action('woocommerce_cart_totals_after_order_total', 'show_discount_info');

function show_discount_info() {
    if (WC()->cart->has_discount()) {
        $discounts = WC()->cart->get_coupons();
        echo '<tr class="discount-info"><th>使用中のクーポン:</th><td>' . implode(', ', $discounts) . '</td></tr>';
    }
}

サンプルコード5:顧客への注意事項の表示

このコードは、カート合計の後に顧客への注意事項を表示します。

add_action('woocommerce_cart_totals_after_order_total', 'display_customer_notice');

function display_customer_notice() {
    echo '<tr class="customer-notice"><th>お知らせ:</th><td>配送までに最大5営業日かかる場合があります。</td></tr>';
}

この関数について質問する


上の計算式の答えを入力してください