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

概要

woocommerce_order_item_visibleフィルタは、WooCommerceでオーダーアイテムの表示状態を制御するために使用されます。具体的には、特定の条件に基づいて注文のアイテムを非表示にする機能を実装する際に役立ちます。一般的に、このフィルタは以下のような機能に使用されます。

  1. 特定のユーザーグループのためにアイテムを隠す
  2. 特定の製品カテゴリに基づいてアイテムを表示/非表示にする
  3. 購入履歴に基づいてアイテムをフィルタリングする
  4. ログインしていないユーザーに対してアイテムを隠す
  5. プロモーションやセールが終了した後にアイテムを隠す
  6. カスタムフィールドの値に応じてアイテムを非表示にする

構文

add_filter( 'woocommerce_order_item_visible', 'your_function_name', 10, 2 );

パラメータ

  • $visible (bool): オーダーアイテムが可視かどうかを示すブール値
  • $item_id (int): オーダーアイテムのID

戻り値

このフィルタは、オーダーアイテムが表示されるかどうかを決定するブール値を返します。

使用可能なバージョン

  • WooCommerceのバージョン: 3.0+
  • WordPressのバージョン: 4.0+

サンプルコード

サンプルコード 1: 特定のユーザーがアイテムを見えなくする

このコードでは、特定のユーザーIDを持つユーザーに対してオーダーアイテムを非表示にします。

add_filter( 'woocommerce_order_item_visible', 'hide_order_item_for_user', 10, 2 );
function hide_order_item_for_user( $visible, $item_id ) {
    if ( is_user_logged_in() && get_current_user_id() === 1 ) {
        return false;  // ユーザーIDが1の場合、アイテムを非表示
    }
    return $visible;
}

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

サンプルコード 2: 製品カテゴリに基づいてアイテムを非表示にする

このコードは、特定の製品カテゴリに属するアイテムを非表示にします。

add_filter( 'woocommerce_order_item_visible', 'hide_item_by_category', 10, 2 );
function hide_item_by_category( $visible, $item_id ) {
    $item = wc_get_order_item( $item_id );
    if ( has_term( 'hidden-category', 'product_cat', $item->get_product_id() ) ) {
        return false;  // 'hidden-category'に属する商品を非表示
    }
    return $visible;
}

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

サンプルコード 3: 購入履歴に基づく非表示設定

このサンプルでは、過去に購入したアイテム以外を隠します。

add_filter( 'woocommerce_order_item_visible', 'hide_unpurchased_items', 10, 2 );
function hide_unpurchased_items( $visible, $item_id ) {
    $user_id = get_current_user_id();
    if ( $user_id ) {
        $purchased_items = wc_get_orders( array(
            'customer_id' => $user_id,
            'limit'       => -1,
            'return'      => 'ids',
        ));
        if ( !in_array( $item_id, $purchased_items ) ) {
            return false; // 購入していないアイテムを非表示
        }
    }
    return $visible;
}

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

サンプルコード 4: ログインしていないユーザー向けの設定

ログインしていないユーザーには全てのアイテムを隠します。

add_filter( 'woocommerce_order_item_visible', 'hide_items_for_non_logged_in_users', 10, 2 );
function hide_items_for_non_logged_in_users( $visible, $item_id ) {
    if ( ! is_user_logged_in() ) {
        return false; // ログインしていない場合、アイテムを非表示
    }
    return $visible;
}

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

サンプルコード 5: プロモーション終了後の非表示設定

プロモーションが終了したアイテムを非表示にします。

add_filter( 'woocommerce_order_item_visible', 'hide_items_after_promotion', 10, 2 );
function hide_items_after_promotion( $visible, $item_id ) {
    $end_date = get_post_meta( $item_id, '_promotion_end_date', true ); // 終了日の取得
    if ( strtotime( $end_date ) < time() ) {
        return false; // 終了日を過ぎていたらアイテムを非表示
    }
    return $visible;
}

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

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

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

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


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