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

概要

woocommerce_delete_order_items アクションは、WooCommerce でオーダーのアイテムが削除されたときにトリガーされます。このフックは、オーダーアイテムの削除プロセスを拡張したり、カスタム処理を追加したりする際に役立ちます。具体的には、次のような場面でよく使用されます:

  1. 削除されたアイテムに関連するカスタムデータのクリーニング。
  2. アイテム削除時に通知メールを送信。
  3. 削除されたアイテムに基づいてレポートを更新。
  4. 外部システムへのデータ同期。
  5. 削除されたアイテムに対するカスタムログの作成。
  6. 削除アクションに対するトリガーとしての使用。

構文

do_action( 'woocommerce_delete_order_items', $order_id, $line_items );

パラメータ

  • $order_id (int): 削除されたアイテムが所属するオーダーのID。
  • $line_items (array): 削除されたアイテムの詳細を含む配列。

戻り値

このアクションは戻り値を持っていません。

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

  • WooCommerce バージョン: 4.0.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_delete_order_items', 'log_deleted_order_items', 10, 2 );

function log_deleted_order_items( $order_id, $line_items ) {
    foreach ( $line_items as $item ) {
        error_log( 'Deleted Item: ' . $item['name'] . ' from Order ID: ' . $order_id );
    }
}

引用元: WordPress Codex

サンプルコード 2: 削除時に通知メールを送信

このコードは、アイテムが削除されたときに管理者へ通知メールを送信します。

add_action( 'woocommerce_delete_order_items', 'send_notification_on_item_delete', 10, 2 );

function send_notification_on_item_delete( $order_id, $line_items ) {
    $to = get_option( 'admin_email' );
    $subject = 'Order Item Deleted';
    $message = 'Items have been deleted from Order ID: ' . $order_id;

    wp_mail( $to, $subject, $message );
}

引用元: WordPress Codex

サンプルコード 3: 自社のCRMシステムに情報を送信

このコードは、削除されたアイテムの情報を自社のCRMに送信します。

add_action( 'woocommerce_delete_order_items', 'send_to_crm_on_item_delete', 10, 2 );

function send_to_crm_on_item_delete( $order_id, $line_items ) {
    foreach ( $line_items as $item ) {
        // 自社のCRM APIへのリクエストを送信
        wp_remote_post( 'https://yourcrm.com/api/delete_item', [
            'body' => json_encode( [
                'order_id' => $order_id,
                'item_name' => $item['name']
            ]),
            'headers' => [
                'Content-Type' => 'application/json',
            ],
        ]);
    }
}

引用元: WordPress Codex

サンプルコード 4: 削除されたアイテムの履歴をデータベースに保存

このコードは、削除されたアイテムの履歴をカスタムテーブルに保存します。

add_action( 'woocommerce_delete_order_items', 'save_deleted_item_history', 10, 2 );

function save_deleted_item_history( $order_id, $line_items ) {
    global $wpdb;
    foreach ( $line_items as $item ) {
        $wpdb->insert( 'wp_deleted_order_items', [
            'order_id' => $order_id,
            'item_name' => $item['name'],
            'deleted_at' => current_time( 'mysql' ),
        ]);
    }
}

引用元: WordPress Codex

サンプルコード 5: 削除アイテムに対するアラートメッセージ

このコードは、削除されたアイテムが特定の基準を満たす場合にアラートメッセージを表示します。

add_action( 'woocommerce_delete_order_items', 'alert_on_critical_item_delete', 10, 2 );

function alert_on_critical_item_delete( $order_id, $line_items ) {
    foreach ( $line_items as $item ) {
        if ( $item['id'] == 123 ) { // 特定のアイテムID
            echo '<script>alert("Critical Item Deleted from Order ID: ' . $order_id . '");</script>';
        }
    }
}

引用元: WordPress Codex

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


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