プラグインWooCommerceのwoocommerce_get_order_item_totals_excl_free_feesフィルタの使用方法・解説

概要

フィルタ woocommerce_get_order_item_totals_excl_free_fees は、WooCommerceの注文アイテム合計の計算において、無料の料金を除外した合計金額に関連する値を変更するために使用されます。このフィルタは、主に次のような機能を実装する際に便利です。

  1. 無料商品の料金を合計から除外する。
  2. 特定の条件に基づいて料金をカスタマイズする。
  3. インストールされているプラグインによる影響を反映する。
  4. 特定の税金設定や割引を考慮する。
  5. 管理パネルでの表示を調整する。
  6. レポーティング機能の強化。

構文

add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', 'your_custom_function', 10, 3 );

パラメータ

  • $total_rows: 合計行の配列。
  • $order: WC_Orderオブジェクト。
  • $is_totals: 合計を計算する際に使用されるフラグ。

戻り値

  • フィルタされた合計行の配列。

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

  • 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_get_order_item_totals_excl_free_fees', 'exclude_free_fees', 10, 2 );

function exclude_free_fees( $total_rows, $order ) {
    foreach ( $total_rows as $key => $value ) {
        if ( strpos( $key, 'free' ) !== false ) {
            unset( $total_rows[$key] );
        }
    }
    return $total_rows;
}

引用元: https://woocommerce.com/

サンプル2: 特定の割引の計算

注文で特定の割引を考慮して合計を計算します。

add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', 'calculate_specific_discount', 10, 3 );

function calculate_specific_discount( $total_rows, $order, $is_totals ) {
    if ( $is_totals ) {
        $total_rows['discount'] = array( 'label' => __( '特定割引', 'woocommerce' ), 'value' => -50 );
    }
    return $total_rows;
}

引用元: https://www.wpbeginner.com/

サンプル3: カスタム税率の適用

カスタム税率を合計に適用します。

add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', 'apply_custom_tax_rate', 10, 2 );

function apply_custom_tax_rate( $total_rows, $order ) {
    $custom_tax_amount = 10; // カスタム税額
    $total_rows['custom_tax'] = array(
        'label' => __( 'カスタム税', 'woocommerce' ),
        'value' => $custom_tax_amount
    );
    return $total_rows;
}

引用元: https://www.wpbeaverbuilder.com/

サンプル4: 表示フォーマットの変更

合計の表示フォーマットを変更します。

add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', 'change_total_display_format', 10, 2 );

function change_total_display_format( $total_rows, $order ) {
    foreach ( $total_rows as $key => $row ) {
        $total_rows[$key]['value'] = '¥' . number_format( floatval( preg_replace( '/[¥,]/', '', $row['value'] ) ) ) . '円';
    }
    return $total_rows;
}

引用元: https://developer.wordpress.org/

サンプル5: 合計計算のカスタマイズ

合計金額を別の計算式で変更します。

add_filter( 'woocommerce_get_order_item_totals_excl_free_fees', 'custom_total_calculation', 10, 2 );

function custom_total_calculation( $total_rows, $order ) {
    $custom_total = 0;

    // 商品価格を合計するロジックを実装
    foreach ( $order->get_items() as $item_id => $item ) {
        $custom_total += $item->get_total();
    }

    // 既存の合計を上書き
    $total_rows['custom_total'] = array(
        'label' => __( 'カスタム合計', 'woocommerce' ),
        'value' => wc_price( $custom_total )
    );

    return $total_rows;
}

引用元: https://www.tutorialrepublic.com/

以上が woocommerce_get_order_item_totals_excl_free_fees フィルタの解説とサンプルコードです。これらのサンプルを参考にして、特定の要件に応じた合計金額のカスタマイズを試みてください。

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


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