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

概要

woocommerce_order_items_meta_display フィルタは、WooCommerce の注文アイテムメタデータの表示をカスタマイズするために使用されます。このフィルタを活用することで、商品や注文に関連するさまざまなメタ情報の出力内容を変更することができます。このフィルタは、次のような機能を実装する際によく使われます。

  1. 商品のカスタムフィールドを表示
  2. 注文した商品のオプションや特徴を強調
  3. 特定の条件に基づいてメタデータのフォーマットを変更
  4. メタ情報の言語翻訳や書式変更
  5. SKUやバーコードなどの追加情報を表示
  6. ショッピング体験を向上させるためのデザイン変更

構文

add_filter( 'woocommerce_order_items_meta_display', 'your_custom_function', 10, 3 );

パラメータ

  • $formatted_meta (array): フォーマットされたメタデータの配列
  • $item_id (int): 注文アイテムのID
  • $order (WC_Order): 注文オブジェクト

戻り値

  • (array): フィルタ後のカスタマイズされたメタデータ配列

使用可能なバージョン

  • 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: カスタムフィールドの追加

このサンプルコードは、注文アイテムのメタデータにカスタムフィールドを追加します。

add_filter( 'woocommerce_order_items_meta_display', 'add_custom_field_meta', 10, 3 );

function add_custom_field_meta( $formatted_meta, $item_id, $order ) {
    $custom_field_value = get_post_meta( $item_id, '_custom_field_key', true );
    if ( $custom_field_value ) {
        $formatted_meta[] = '<strong>Custom Field:</strong> ' . esc_html( $custom_field_value );
    }
    return $formatted_meta;
}

引用元: https://www.example.com/sample1

サンプル 2: メタデータのフォーマット変更

このサンプルコードは、特定の条件に基づいてメタデータの出力フォーマットを変更します。

add_filter( 'woocommerce_order_items_meta_display', 'change_meta_format', 10, 3 );

function change_meta_format( $formatted_meta, $item_id, $order ) {
    // 条件が特定のプロダクトIDの場合
    if ( $order->get_item( $item_id )->get_product_id() === 123 ) {
        foreach ( $formatted_meta as &$meta ) {
            $meta = '<em>' . $meta . '</em>'; // メタデータをイタリック体に
        }
    }
    return $formatted_meta;
}

引用元: https://www.example.com/sample2

サンプル 3: SKUの表示

このサンプルコードでは、商品SKUを注文アイテムメタに追加しています。

add_filter( 'woocommerce_order_items_meta_display', 'display_product_sku', 10, 3 );

function display_product_sku( $formatted_meta, $item_id, $order ) {
    $product = $order->get_item( $item_id )->get_product();
    if ( $product ) {
        $sku = $product->get_sku();
        if ( $sku ) {
            $formatted_meta[] = '<strong>SKU:</strong> ' . esc_html( $sku );
        }
    }
    return $formatted_meta;
}

引用元: https://www.example.com/sample3

サンプル 4: 特別なメッセージの追加

このサンプルコードでは、特別なメッセージをメタデータに追加します。

add_filter( 'woocommerce_order_items_meta_display', 'add_special_message', 10, 3 );

function add_special_message( $formatted_meta, $item_id, $order ) {
    $formatted_meta[] = '<strong>Thank you for your purchase!</strong>';
    return $formatted_meta;
}

引用元: https://www.example.com/sample4

サンプル 5: 翻訳の追加

このサンプルコードでは、メタデータの出力を翻訳可能なフォーマットにします。

add_filter( 'woocommerce_order_items_meta_display', 'translatable_meta_display', 10, 3 );

function translatable_meta_display( $formatted_meta, $item_id, $order ) {
    $formatted_meta[] = '<strong>' . __( 'Order processed:', 'your-text-domain' ) . '</strong> ' . esc_html( $order->get_order_number() );
    return $formatted_meta;
}

引用元: https://www.example.com/sample5

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


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