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

概要

woocommerce_product_import_inserted_product_object は、WooCommerce の製品データをインポートする際に、インポートされた製品オブジェクトが挿入された後にトリガーされるフックです。このフックは、特定のアクションを追加するためのポイントを提供します。主に以下のような機能を実装する際に使用されます。

  1. カスタムフィールドの追加
  2. 製品のレビューの自動作成
  3. インポートされた製品のステータス変更
  4. 外部APIへのデータ送信
  5. イベントログの作成
  6. 密接な管理者通知の送信

このアクションの構文は以下の通りです。

do_action( 'woocommerce_product_import_inserted_product_object', $product, $data, $importer );

パラメータ

  • $product: 新しく挿入された製品のオブジェクト。
  • $data: インポートに使用された元データ。
  • $importer: 製品をインポートする際に利用されるインポータオブジェクト。

戻り値

このアクションには戻り値はありません。これは、処理や処理後のアクションを実行するためのフックとして機能します。

対応するバージョン

  • WooCommerce バージョン: 4.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_product_import_inserted_product_object', 'add_custom_field_to_product', 10, 3 );

function add_custom_field_to_product( $product, $data, $importer ) {
    $custom_field_value = isset( $data['custom_field'] ) ? $data['custom_field'] : '';
    $product->update_meta_data( 'custom_field_key', $custom_field_value );
    $product->save();
}

このサンプルは、製品がインポートされた後にカスタムフィールドを追加するものです。

サンプル 2: 製品のレビュー自動作成

add_action( 'woocommerce_product_import_inserted_product_object', 'create_product_review', 10, 3 );

function create_product_review( $product, $data, $importer ) {
    if ( $product instanceof WC_Product ) {
        $review_data = array(
            'comment_post_ID' => $product->get_id(),
            'comment_content' => '自動生成レビュー',
            'comment_type'    => 'review',
            'comment_author'  => '自動ユーザー',
            'comment_rating'  => 5,
            'comment_approved'=> 1,
        );
        wp_insert_comment( $review_data );
    }
}

このコードは、製品がインポートされた際に自動的にレビューを作成します。

サンプル 3: 製品ステータスの変更

add_action( 'woocommerce_product_import_inserted_product_object', 'change_product_status', 10, 3 );

function change_product_status( $product, $data, $importer ) {
    if ( ! empty( $data['status'] ) ) {
        $product->set_status( $data['status'] );
        $product->save();
    }
}

このサンプルは、インポートされた製品のステータスを変更するものです。

サンプル 4: 外部APIへのデータ送信

add_action( 'woocommerce_product_import_inserted_product_object', 'send_product_to_external_api', 10, 3 );

function send_product_to_external_api( $product, $data, $importer ) {
    $api_url = 'https://example.com/api/products';
    $response = wp_remote_post( $api_url, array(
        'body' => json_encode( array(
            'id' => $product->get_id(),
            'name' => $product->get_name(),
        )),
        'headers' => array( 'Content-Type' => 'application/json' )
    ));
}

このコードは、インポートされた製品の情報を外部APIに送信するものです。

サンプル 5: 管理者への通知送信

add_action( 'woocommerce_product_import_inserted_product_object', 'notify_admin_on_product_import', 10, 3 );

function notify_admin_on_product_import( $product, $data, $importer ) {
    $to = get_option( 'admin_email' );
    $subject = '新しい製品がインポートされました';
    $message = '製品名: ' . $product->get_name();
    wp_mail( $to, $subject, $message );
}

このサンプルは、製品がインポートされたことを管理者に通知するメールを送信します。

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


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