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

概要

woocommerce_order_items_meta_get_formatted フィルタは、WooCommerceで注文アイテムのメタデータをフォーマットする際に利用されるフックです。このフィルタを使用することで、注文商品ごとのメタデータの表示形式や内容を調整することができます。主に以下のような機能を実装する際に使用されます。

  1. 注文アイテムのカスタムメタデータのフォーマットを変更する。
  2. 商品のオプションやバリエーション情報を見やすく整形する。
  3. 特別なプロモーションやディスカウント情報を注文詳細に追加する。
  4. 予約商品やデジタル商品のメタ情報をカスタマイズする。
  5. 商品ごとの価格や数量の表示形式を変更する。
  6. 特定の条件に基づいて追加情報を表示する。

構文

add_filter('woocommerce_order_items_meta_get_formatted', 'your_function_name', 10, 3);

パラメータ

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

戻り値

  • array: 修正されたフォーマットされたメタデータの配列。

使用可能なバージョン

  • WooCommerce バージョン: 3.0.0 以降
  • WordPress バージョン: 4.0 以降

サンプルコード

サンプルコード1: カスタムメタデータの表示

このコードは、注文アイテムにカスタムメタデータを追加して表示します。

add_filter('woocommerce_order_items_meta_get_formatted', 'add_custom_meta_to_order_items', 10, 3);
function add_custom_meta_to_order_items($formatted_meta, $item_id, $order) {
    $custom_meta = get_post_meta($item_id, '_custom_meta_key', true);
    if ($custom_meta) {
        $formatted_meta[] = sprintf('<p><strong>%s:</strong> %s</p>', __('Custom Meta'), esc_html($custom_meta));
    }
    return $formatted_meta;
}

引用元: https://www.businessbloomer.com/

サンプルコード2: 特別情報の追加

このコードは注文アイテムに特別なディスカウント情報を追加します。

add_filter('woocommerce_order_items_meta_get_formatted', 'add_discount_info_to_order_items', 10, 3);
function add_discount_info_to_order_items($formatted_meta, $item_id, $order) {
    $discount = get_post_meta($item_id, '_item_discount', true);
    if (!empty($discount)) {
        $formatted_meta[] = sprintf('<p><strong>%s:</strong> -%s</p>', __('Discount'), wc_price($discount));
    }
    return $formatted_meta;
}

引用元: https://docs.woocommerce.com/

サンプルコード3: 商品バリエーション情報のフォーマット

このコードは、バリエーション情報を整形して表示します。

add_filter('woocommerce_order_items_meta_get_formatted', 'format_variation_attributes', 10, 3);
function format_variation_attributes($formatted_meta, $item_id, $order) {
    $item = $order->get_item($item_id);
    $variation_attributes = $item->get_variation_attributes();
    if (!empty($variation_attributes)) {
        foreach ($variation_attributes as $attribute_name => $attribute_value) {
            $formatted_meta[] = sprintf('<p><strong>%s:</strong> %s</p>', wc_attribute_label($attribute_name), esc_html($attribute_value));
        }
    }
    return $formatted_meta;
}

引用元: https://woocommerce.com/

サンプルコード4: 予約商品のメタ情報を追加

このコードは、予約商品に追加のメタ情報を表示します。

add_filter('woocommerce_order_items_meta_get_formatted', 'add_booking_details', 10, 3);
function add_booking_details($formatted_meta, $item_id, $order) {
    $is_booking = get_post_meta($item_id, '_is_booking', true);
    if ($is_booking) {
        $date = get_post_meta($item_id, '_booking_date', true);
        $formatted_meta[] = sprintf('<p><strong>%s:</strong> %s</p>', __('Booking Date'), esc_html($date));
    }
    return $formatted_meta;
}

引用元: https://woocommerce.com/

サンプルコード5: 特定条件での情報表示

このコードは、特定の条件に基づいてメタ情報を表示します。

add_filter('woocommerce_order_items_meta_get_formatted', 'conditional_meta_display', 10, 3);
function conditional_meta_display($formatted_meta, $item_id, $order) {
    if ($order->get_total() > 100) {
        $formatted_meta[] = '<p><strong>' . __('Thanks for your order over $100!') . '</strong></p>';
    }
    return $formatted_meta;
}

引用元: https://wordpress.org/

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

アクション 使用可能性
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

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


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