概要
woocommerce_order_item_fee_after_calculate_taxes
は、WooCommerceにおいて、注文アイテムの料金が税金を計算した後に実行されるフックです。このフックを使用することで、特定の料金に対するカスタマイズや追加のロジックを実装することができます。このアクションは、割引や手数料の計算を行う際によく使われます。
一般的な利用ケースとしては以下のようなものがあります。
1. 特定の手数料を追加する。
2. 割引の計算を調整する。
3. 料金に条件を基づくロジックを適用する。
4. 複数通貨サポートを追加する。
5. 顧客グループに基づく異なる税率を適用する。
6. 特定の製品カテゴリに対する特別な手数料を追加する。
構文
do_action( 'woocommerce_order_item_fee_after_calculate_taxes', $item_id, $fee, $order, $taxes );
パラメータ
$item_id
(int): アイテムのID$fee
(WC_Order_Item_Fee): 手数料オブジェクト$order
(WC_Order): 注文オブジェクト$taxes
(array): 税金の情報が含まれる配列
戻り値
このアクション自体には戻り値はありませんが、登録されたコールバック関数内でカスタマイズされたデータを返すことができます。
使用可能なプラグイン
このアクションは、WooCommerceのバージョン3.0.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 |
サンプルコード
-
手数料に固定の税率を適用する
add_action('woocommerce_order_item_fee_after_calculate_taxes', 'apply_fixed_tax_rate', 10, 4); function apply_fixed_tax_rate($item_id, $fee, $order, $taxes) { if ($fee->get_name() === 'Custom Fee') { // 固定税率を適用 $taxes[0] = 2; // 例: 固定で2の税 $fee->set_total($fee->get_total() + $taxes[0]); } }
このコードは、特定の手数料「Custom Fee」に固定の税金を追加します。
-
手数料に条件を基づく追加コストを設定する
add_action('woocommerce_order_item_fee_after_calculate_taxes', 'add_conditional_fee', 10, 4); function add_conditional_fee($item_id, $fee, $order, $taxes) { if ($order->get_total() > 100) { $fee->set_total($fee->get_total() + 5); // 100ドル以上で5ドルの料金を追加 } }
このコードは、総額が100ドルを超える場合に追加料金を適用します。
-
特定の顧客グループへの割引を適用する
add_action('woocommerce_order_item_fee_after_calculate_taxes', 'apply_group_discount', 10, 4); function apply_group_discount($item_id, $fee, $order, $taxes) { if (user_can($order->get_user_id(), 'wholesale_customer')) { $fee->set_total($fee->get_total() - 10); // 卸売顧客に10ドル割引 } }
このコードは、卸売顧客に割引を適用します。
-
特定の製品カテゴリの手数料を変更する
add_action('woocommerce_order_item_fee_after_calculate_taxes', 'modify_fee_for_specific_category', 10, 4); function modify_fee_for_specific_category($item_id, $fee, $order, $taxes) { $items = $order->get_items(); foreach ($items as $item) { if (has_term('specific-category', 'product_cat', $item->get_product_id())) { $fee->set_total($fee->get_total() + 3); // 特定カテゴリの製品に3ドル追加 } } }
このコードは、特定の製品カテゴリアイテムがある場合に手数料を増加させます。
-
手数料の総額に基づいた税金を修正する
add_action('woocommerce_order_item_fee_after_calculate_taxes', 'adjust_tax_based_on_fee', 10, 4); function adjust_tax_based_on_fee($item_id, $fee, $order, $taxes) { if ($fee->get_total() > 50) { $taxes[0] += 1; // 手数料が50ドルを超える場合、税金を1ドル増加 } }
このコードは、手数料が50ドルを超える場合に税金を調整します。