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

概要

woocommerce_email_classes フィルタは、WooCommerce のメールクラスをカスタマイズするために使用されます。このフックは、WooCommerce がメールを送信する際に、デフォルトのメールクラスから派生した独自のメールクラスを追加、削除、または変更することができます。このフィルタは、次のような機能を実装する際によく使われます。

  1. 独自のメールテンプレートの作成
  2. メールのコンテンツをカスタマイズ
  3. 特定の条件に基づくメールの送信
  4. 管理者または顧客専用のメール通知の追加
  5. メールのスタイルやレイアウトの変更
  6. フィルタリングされた情報に基づいた統計データの送信

構文

add_filter('woocommerce_email_classes', 'custom_email_classes');

パラメータ

  • $email_classes: WooCommerce のメールクラスの配列。

戻り値

  • $email_classes: 編集後のメールクラスの配列。

使用可能なバージョン

  • WooCommerce バージョン: 2.1 以上
  • 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: 独自のメールクラスを追加する

このサンプルコードは、新しいメールクラス「WC_Email_Custom」を WooCommerce のメールクラスに追加します。

add_filter('woocommerce_email_classes', 'add_custom_email_class');

function add_custom_email_class($email_classes) {
    class WC_Email_Custom extends WC_Email {
        public function __construct() {
            $this->id = 'custom_email';
            $this->title = 'Custom Email';
            $this->description = 'Custom email description';
            $this->template_base = plugin_dir_path(__FILE__) . 'templates/';
            $this->template_html = 'custom-email.php';
            $this->template_plain = 'custom-email-plain.php';
            parent::__construct();
        }
        public function trigger($order_id) {
            $this->object = wc_get_order($order_id);
            if ($this->object) {
                $this->recipient = $this->object->get_billing_email();
                parent::trigger($order_id);
            }
        }
    }
    $email_classes['WC_Email_Custom'] = new WC_Email_Custom();
    return $email_classes;
}

引用元: 無し

サンプル2: メールタイトルのカスタマイズ

このサンプルコードは、送信されるメールのタイトルをカスタマイズします。

add_filter('woocommerce_email_subject_new_order', 'custom_new_order_email_subject', 10, 2);

function custom_new_order_email_subject($subject, $order) {
    return '新規注文: ' . $order->get_order_number();
}

引用元: 無し

サンプル3: HTML メールのスタイルを追加する

このサンプルコードは、送信される HTML メールに独自のスタイルを追加します。

add_filter('woocommerce_email_styles', 'add_custom_email_styles');

function add_custom_email_styles($css) {
    $css .= 'body { font-family: Arial, sans-serif; }';
    return $css;
}

引用元: 無し

サンプル4: 特定のメールを送信しない

このコードは、特定の条件下でメールの送信をキャンセルする例です。

add_filter('woocommerce_email_enabled_new_order', 'disable_new_order_email', 10, 2);

function disable_new_order_email($enabled, $order) {
    if ($order->get_total() < 50) {
        return false; // 合計が50未満の場合、メールを送信しない
    }
    return $enabled;
}

引用元: 無し

サンプル5: メールテンプレートの地址を変更する

このサンプルコードは、デフォルトのメールテンプレートのパスを変更します。

add_filter('woocommerce_email_templates', 'custom_email_template_path');

function custom_email_template_path($template) {
    $template['new_order'] = '/path/to/your/custom/new-order-template.php';
    return $template;
}

引用元: 無し

上記のサンプルコードを使って、WooCommerce のメールクラスを様々にカスタマイズすることができます。それぞれのニーズに応じて適切に変更してください。

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


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