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

概要

woocommerce_order_item_after_calculate_taxes アクションは、WooCommerceでの注文アイテムの税計算が完了した後に発火します。このアクションは、特定のカスタム処理やデータの追加を行いたい場合に便利です。以下のようなシナリオで使用されることが一般的です:

  1. カスタム税率の適用
  2. 特定の商品に基づく割引の計算
  3. 注文アイテムにカスタムメタデータを追加
  4. 独自のレポート生成
  5. 特定の条件に基づく通知の送信
  6. 統計データの収集

構文

do_action('woocommerce_order_item_after_calculate_taxes', $item_id, $order, $taxes);

パラメータ

  • $item_id (int): 注文アイテムのID
  • $order (WC_Order): 現在の注文オブジェクト
  • $taxes (array): 税情報の配列

戻り値

このアクションは値を返しません。

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

  • WooCommerce 2.7以降

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

  • WordPress 4.0以降

サンプルコード

サンプル1: カスタムメタデータを追加する

このサンプルコードは、注文アイテムにカスタムメタデータを保存する方法を示しています。

add_action('woocommerce_order_item_after_calculate_taxes', 'custom_add_meta_after_taxes', 10, 3);
function custom_add_meta_after_taxes($item_id, $order, $taxes) {
    $custom_value = 'カスタムデータ';
    wc_add_order_item_meta($item_id, '_custom_meta_key', $custom_value);
}

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

サンプル2: 特定の税率に基づいて割引を適用する

このコードは、特定の税率が適用された場合に割引を計算します。

add_action('woocommerce_order_item_after_calculate_taxes', 'apply_discount_based_on_tax', 10, 3);
function apply_discount_based_on_tax($item_id, $order, $taxes) {
    if (isset($taxes['total']) && $taxes['total'] > 50) {
        $discount = $taxes['total'] * 0.1; // 10%の割引
        $order->set_discount_total($order->get_discount_total() + $discount);
    }
}

引用元: https://woocommerce.wp-a2z.org/

サンプル3: 注文が確定された後に通知を送信

このサンプルは、特定の条件に基づいて注文確定後に通知メールを送信します。

add_action('woocommerce_order_item_after_calculate_taxes', 'send_notification_after_order', 10, 3);
function send_notification_after_order($item_id, $order, $taxes) {
    if ($order->get_total() > 100) {
        wp_mail('example@example.com', '新しい大口注文', '注文ID: ' . $order->get_id());
    }
}

引用元: https://www.satollo.net/

サンプル4: 売上データを記録する

このコードは、売上データをカスタムテーブルに記録します。

add_action('woocommerce_order_item_after_calculate_taxes', 'record_sales_data', 10, 3);
function record_sales_data($item_id, $order, $taxes) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'sales_data';

    $wpdb->insert($table_name, array(
        'item_id' => $item_id,
        'order_id' => $order->get_id(),
        'tax_total' => $taxes['total'],
        'created_at' => current_time('mysql'),
    ));
}

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

サンプル5: 特定の商品に対してカスタム税を適用する

このサンプルコードは、特定の商品にカスタム税を適用する方法を示しています。

add_action('woocommerce_order_item_after_calculate_taxes', 'apply_custom_tax_to_specific_product', 10, 3);
function apply_custom_tax_to_specific_product($item_id, $order, $taxes) {
    $item_product_id = wc_get_order_item_product_id($item_id);

    if ($item_product_id === 12345) { // 商品IDが12345の場合
        $custom_tax = 5.00; // カスタム税額
        $taxes['total'] += $custom_tax; // 合計税額に追加
    }
}

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

この関数のアクションでの使用可能性

アクション 使用例
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

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


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