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

概要

woocommerce_email_order_item_quantity フィルタは、WooCommerce のメールにおける注文アイテムの数量表示を変更するために使用されます。このフィルタは、特定の状況で数量をカスタマイズしたり、特定の条件に基づいて異なるフォーマットで表示したりするために役立ちます。

主に以下のような機能を実装する際に使われます:
1. 注文メールのデザインをカスタマイズする。
2. 商品の数量表示を条件に応じて変更する。
3. 特定の製品に対して数量を特別に表示する。
4. 数量に特別なマークやアイコンを追加する。
5. 複数のアイテムがある場合のフォーマットを調整する。
6. 注文アイテムの説明に数量情報を統合する。

構文

add_filter('woocommerce_email_order_item_quantity', 'custom_function_name', 10, 3);

パラメータ

  • string $quantity: 表示される数量の文字列。
  • object $item: 注文アイテムのオブジェクト。
  • object $order: 注文のオブジェクト。

戻り値

フィルタによって変更された数量の文字列。

使用可能なバージョン

  • WooCommerce: 3.0.0以上
  • WordPress: 4.0.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_filter('woocommerce_email_order_item_quantity', 'custom_quantity_format', 10, 3);

function custom_quantity_format($quantity, $item, $order) {
    return $quantity . ' pcs'; // 数量の後に「pcs」を追加
}

このコードは、注文アイテムの数量の後に「pcs」を追加して表示します。

サンプルコード2

add_filter('woocommerce_email_order_item_quantity', 'highlight_special_item_quantity', 10, 3);

function highlight_special_item_quantity($quantity, $item, $order) {
    if ($item->get_product_id() === 123) { // 特定の製品IDの場合
        return '<strong>' . $quantity . '</strong>'; // 数量を強調表示
    }
    return $quantity;
}

このコードは、特定の製品の数量を強調表示します。

サンプルコード3

add_filter('woocommerce_email_order_item_quantity', 'conditionally_format_quantity', 10, 3);

function conditionally_format_quantity($quantity, $item, $order) {
    if ($quantity > 10) {
        return '<span style="color:red;">' . $quantity . '</span>'; // 10より多い場合は赤色で表示
    }
    return $quantity;
}

このコードは、数量が10を超える場合に赤色で表示します。

サンプルコード4

add_filter('woocommerce_email_order_item_quantity', 'append_discount_info_to_quantity', 10, 3);

function append_discount_info_to_quantity($quantity, $item, $order) {
    $discounted_price = $item->get_total() / $quantity; // 割引後の価格を計算
    return $quantity . ' (Discounted Price: ' . wc_price($discounted_price) . ')'; // 割引後の価格を追加
}

このコードは、数量の後に割引後の価格を表示します。

サンプルコード5

add_filter('woocommerce_email_order_item_quantity', 'customize_quantity_based_on_order_status', 10, 3);

function customize_quantity_based_on_order_status($quantity, $item, $order) {
    if ($order->get_status() === 'completed') { // 完了した注文の場合
        return 'Completed: ' . $quantity; // 完了したことを示すテキストを追加
    }
    return $quantity;
}

このコードは、注文が完了した場合に数量の前に「Completed:」というテキストを追加します。

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


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