概要
woocommerce_order_item_quantity
フィルタは、WooCommerce プラグインの中で利用され、特定の注文アイテムの数量を変更またはフィルタリングする機能を提供します。このフィルタは、商品がカートに追加された際や、注文が処理される際の数量の表示に関与しています。以下のような機能を実装する際によく使われます。
- 注文アイテムの数量をカスタマイズ
- 特定の条件に基づく数量の表示変更
- 数量の単位をカスタマイズ(例:個、箱など)
- 割引やプロモーションに応じた数量の調整
- フロントエンドでの数量のカスタマイズ表示
- 管理画面での注文詳細表示のカスタマイズ
構文
add_filter('woocommerce_order_item_quantity', 'custom_function', 10, 2);
パラメータ
$quantity
– 注文アイテムの数量(整数)$order_item
– WC_Order_Item_Product オブジェクト(注文アイテム)
戻り値
変更された数量(整数)
バージョン情報
- WooCommerceバージョン: 2.1 以降
- WordPressバージョン: 4.1 以降
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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', 'modify_order_item_quantity', 10, 2);
function modify_order_item_quantity($quantity, $order_item) {
if ($order_item->get_product_id() == 123) { // 商品IDが123の場合
return $quantity * 2; // 数量を2倍にする
}
return $quantity;
}
このコードは、特定の商品IDに対して数量を2倍に変更します。特定の商品に対する特別な処理を実行する際に役立ちます。
サンプルコード2
add_filter('woocommerce_order_item_quantity', 'format_quantity_label', 10, 2);
function format_quantity_label($quantity, $order_item) {
return $quantity . '個'; // 数量の後に「個」を追加
}
このコードは、表示される数量の後に「個」という単位を追加して、日本語のユーザーに分かりやすくします。
サンプルコード3
add_filter('woocommerce_order_item_quantity', 'adjust_discounted_quantity', 10, 2);
function adjust_discounted_quantity($quantity, $order_item) {
$product = $order_item->get_product();
if ($product->is_on_sale()) {
return $quantity - 1; // セール中の商品の数量を1減少させる
}
return $quantity;
}
このコードは、セール中の商品に対してその数量を1減少させることで、数量の印象を変えます。
サンプルコード4
add_filter('woocommerce_order_item_quantity', 'custom_order_quantity_display', 10, 2);
function custom_order_quantity_display($quantity, $order_item) {
return '(' . $quantity . ')'; // 数量を括弧で囲む
}
このコードは、数量を括弧で囲んで表示させ、視覚的に強調します。
サンプルコード5
add_filter('woocommerce_order_item_quantity', 'conditional_quantity_format', 10, 2);
function conditional_quantity_format($quantity, $order_item) {
if ($quantity > 10) {
return '大量の注文: ' . $quantity; // 10以上の数量の場合の表記
}
return $quantity;
}
このコードは、数量が10を超えた場合に特別なメッセージを追加して表示します。特定の条件によるカスタマイズが可能です。