概要
woocommerce_order_item_quantity_html
フィルタは、WooCommerceの注文アイテムの数量表示を変更するために使用されるフックです。主に以下のような機能を実装する際に利用されます。
- 数量表示のカスタマイズ
- 割引や特別オファーの条件付き表示
- ユーザーへのインセンティブ表示
- アイテムの特定のスタイルに合わせた装飾
- 外部データを統合した表示
- カートや注文確認ページでのインタラクティブな要素の追加
構文
add_filter( 'woocommerce_order_item_quantity_html', 'your_function_name', 10, 2 );
パラメータ
- $quantity_html (string): 元の数量HTML。
- $order_item (WC_Order_Item_Product): 注文アイテムのオブジェクト。
戻り値
- 加工された数量HTML(string)。
使用可能なプラグイン WooCommerce のバージョン
- WooCommerce 2.0.0以降
ワードプレスのバージョン
- 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_item_quantity_html', 'custom_order_item_quantity', 10, 2 );
function custom_order_item_quantity( $quantity_html, $order_item ) {
return '<strong>' . $quantity_html . '</strong>';
}
このサンプルコードは、注文アイテムの数量を太字で表示するカスタマイズを行います。
サンプルコード2: 割引率を条件付けて表示
add_filter( 'woocommerce_order_item_quantity_html', 'show_discount_item_quantity', 10, 2 );
function show_discount_item_quantity( $quantity_html, $order_item ) {
if ( $order_item->get_total() > 100 ) {
return $quantity_html . ' <span style="color:red;">(割引適用)</span>';
}
return $quantity_html;
}
このサンプルコードは、アイテムの合計金額が100を超える場合、特別なメッセージを数量表示の横に付加します。
サンプルコード3: アイテムにスターを表示
add_filter( 'woocommerce_order_item_quantity_html', 'add_star_to_quantity_html', 10, 2 );
function add_star_to_quantity_html( $quantity_html, $order_item ) {
return $quantity_html . ' ★';
}
このサンプルコードは、数量の横に星マークを追加し、特別感を演出します。
サンプルコード4: 複数のアイテムに応じた表示
add_filter( 'woocommerce_order_item_quantity_html', 'conditional_quantity_display', 10, 2 );
function conditional_quantity_display( $quantity_html, $order_item ) {
if ( $order_item->get_item_meta( 'is_combined', true ) ) {
return '合計: ' . $quantity_html;
}
return $quantity_html;
}
このサンプルコードは、特定のアイテムが組み合わされた場合の数量表示をカスタマイズします。
サンプルコード5: Tooltipを追加
add_filter( 'woocommerce_order_item_quantity_html', 'add_tooltip_to_quantity', 10, 2 );
function add_tooltip_to_quantity( $quantity_html, $order_item ) {
return '<span title="この商品の数量">' . $quantity_html . '</span>';
}
このサンプルコードは、数量表示にツールチップを追加して、数量に関する情報を提供します。