プラグインWooCommerceのwoocommerce_order_cancelled_notice_typeフィルタの使用方法・解説

概要

woocommerce_order_cancelled_notice_type フィルタは、WooCommerceにおいて、注文がキャンセルされた際の通知のタイプを変更するために使用されます。このフィルタは、カスタム通知タイプを指定したり、特定の条件に基づいて異なる通知を提供したりする際に便利です。主に以下のような機能を実装する際に役立ちます。

  1. オンラインストアの管理者向けのカスタム通知
  2. 顧客へのキャンセル理由に応じた条件付き通知
  3. 承認されたキャンセル通知のカスタマイズ
  4. 宣伝やプロモーションの一環としての通知形式の変更
  5. サポートチームへの特別な通知
  6. 分析用のデータ収集と通知の調整

このフィルタはWooCommerce 3.0以降、WordPress 4.7以降で使用可能です。

構文

add_filter('woocommerce_order_cancelled_notice_type', 'custom_notice_type', 10, 2);

パラメータ

  • $notice_type: 現在の通知タイプ(文字列)
  • $order: 対象の注文オブジェクト(WC_Order)

戻り値

  • 変更後の通知タイプを返します(文字列)。

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

アクション 使用例
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_filter('woocommerce_order_cancelled_notice_type', 'change_cancelled_notice_type', 10, 2);
function change_cancelled_notice_type($notice_type, $order) {
    return 'custom_cancelled_notification';  // 新しい通知形式を返す
}

このコードは、キャンセルされた注文に対して使用される通知タイプを 'custom_cancelled_notification' に変更します。

サンプルコード 2: 特定の条件で通知タイプを変更

add_filter('woocommerce_order_cancelled_notice_type', 'conditional_cancelled_notice_type', 10, 2);
function conditional_cancelled_notice_type($notice_type, $order) {
    if ($order->get_total() > 100) {
        return 'high_value_cancelled_notification';  // 高額注文取消の場合の通知形式
    }
    return $notice_type;  // それ以外は元の形式を返す
}

このコードは、注文の合計金額が100ドルを超える場合に特別な通知タイプを適用します。

サンプルコード 3: 顧客向け通知タイプの追加

add_filter('woocommerce_order_cancelled_notice_type', 'add_customer_notice_type', 10, 2);
function add_customer_notice_type($notice_type, $order) {
    if ($order->get_user_id() != 0) { // 顧客がいる場合
        return 'customer_cancelled_notification';
    }
    return $notice_type;
}

このコードは、顧客の存在に基づいて異なる通知タイプを提供します。

サンプルコード 4: メッセージ内容に基づいた調整

add_filter('woocommerce_order_cancelled_notice_type', 'message_based_cancelled_notice_type', 10, 2);
function message_based_cancelled_notice_type($notice_type, $order) {
    if ($order->get_status() == 'failed') {
        return 'failed_order_cancel_notification';  // 失敗した注文のための特別な通知
    }
    return $notice_type;
}

この例では、注文が失敗した場合に特別な通知タイプを返します。

サンプルコード 5: API 連携用の通知タイプに変更

add_filter('woocommerce_order_cancelled_notice_type', 'api_integration_notice_type', 10, 2);
function api_integration_notice_type($notice_type, $order) {
    // 外部APIと連携する場合
    return 'api_cancel_notification';  // API用の通知タイプを返す
}

このコードは、外部APIと連携するための特別な通知タイプを指定します。

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


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