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

概要

woocommerce_before_save_order_item アクションは、WooCommerceの注文アイテムがデータベースに保存される直前にトリガーされるフックです。このアクションを使用することで、注文アイテムに対して追加の処理やカスタマイズを実行することが可能になります。たとえば、特定の条件に基づいて値を変更したり、データの検証を行ったりする状況でよく利用されます。また、以下のような機能実装にも使われることがあります。

  1. 注文アイテムのカスタムメタデータの保存
  2. 特定のプロモーションロジックの適用
  3. 在庫の調整
  4. 注文ステータスの変更
  5. 印刷用のレポートの生成
  6. 顧客への通知の設定

構文

add_action('woocommerce_before_save_order_item', 'your_function_name', 10, 2);

パラメータ

  1. $item_id (int) – 保存される注文アイテムのID。
  2. $item (WC_Order_Item) – 保存する注文アイテムのオブジェクト。

戻り値

このアクション自体には戻り値はありませんが、実行される関数内での処理に基づいて効果が反映されます。

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

  • 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_action('woocommerce_before_save_order_item', 'custom_save_order_item_meta', 10, 2);

function custom_save_order_item_meta($item_id, $item) {
    // 注文アイテムのカスタムメタデータを保存
    $custom_meta_value = 'カスタム値';
    wc_add_order_item_meta($item_id, '_custom_meta_key', $custom_meta_value);
}

このコードは、注文アイテムが保存されるときに、カスタムメタデータを追加します。
引用元: https://woocommerce.github.io/code-reference/classes/WC-Order.html#method_wc_add_order_item_meta

サンプルコード 2

add_action('woocommerce_before_save_order_item', 'apply_discount_based_on_quantity', 10, 2);

function apply_discount_based_on_quantity($item_id, $item) {
    $quantity = $item->get_quantity();
    if ($quantity > 5) {
        $item->set_total($item->get_total() * 0.9); // 10%の割引適用
    }
}

このコードは、数量が5以上の注文アイテムに対して、10%の割引を適用します。
引用元: https://woocommerce.github.io/code-reference/classes/WC-Order-Item.html

サンプルコード 3

add_action('woocommerce_before_save_order_item', 'notify_order_item_update', 10, 2);

function notify_order_item_update($item_id, $item) {
    // 注文アイテムの情報をログに出力
    error_log('注文アイテムが更新されました: ' . $item_id);
}

このコードは、注文アイテムが保存されるたびに、アイテム情報をエラーログに記録します。
引用元: https://developer.wordpress.org/reference/functions/error_log/

サンプルコード 4

add_action('woocommerce_before_save_order_item', 'adjust_stock_on_order_item_save', 10, 2);

function adjust_stock_on_order_item_save($item_id, $item) {
    $product_id = $item->get_product_id();
    $quantity = $item->get_quantity();
    $stock = get_post_meta($product_id, '_stock', true);
    update_post_meta($product_id, '_stock', $stock - $quantity);
}

このコードは、注文アイテムが保存されるときに、製品の在庫を調整します。
引用元: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

サンプルコード 5

add_action('woocommerce_before_save_order_item', 'log_item_meta_changes', 10, 2);

function log_item_meta_changes($item_id, $item) {
    $meta_data = $item->get_meta_data();
    error_log('メタデータが変更されました: ' . print_r($meta_data, true));
}

このコードは、注文アイテムのメタデータが変更されたときに、それをエラーログに出力します。
引用元: https://woocommerce.github.io/code-reference/classes/WC-Order-Item.html#method_get_meta_data

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


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