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

概要

woocommerce_attribute_default_visibility フィルタは、WooCommerce において製品の属性のデフォルト可視性を制御するために使用されます。このフィルタを利用することで、特定の属性を持つ製品がフロントエンドでどのように表示されるかをカスタマイズできます。たとえば、属性の可視性を変更したり、特定の条件に基づいて属性を非表示にしたりすることができます。

このフィルタは以下のような場面でよく使用されます。
1. 製品属性の表示/非表示を動的に変更したい。
2. 特定のユーザー役割に基づき、属性の可視性を調整したい。
3. 季節やキャンペーンに応じて、特定の属性を一時的に非表示にしたい。
4. カスタム製品タイプやカスタム属性を使用する場合に、特定の属性のみを表示させたい。
5. プラグインとの衝突を回避するために、特定の属性の可視性を変えたい。
6. 製品のバリエーションが売切れの場合に、関連する属性を非表示にしたい。

このフィルタを使用可能なWooCommerceバージョンは3.0以降、WordPressのバージョンは4.0以降です。

構文

add_filter('woocommerce_attribute_default_visibility', 'custom_function_name', 10, 2);

パラメータ

  • $visible (bool) – 属性の可視性。デフォルトは true
  • $attribute (WC_Product_Attribute) – 対象の属性オブジェクト。

戻り値

  • bool – 属性を表示するかどうかの真偽値。

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

アクション 使用例
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_attribute_default_visibility', 'hide_specific_attribute', 10, 2);

function hide_specific_attribute($visible, $attribute) {
    if ($attribute->get_name() === '体重') { // 属性名が'体重'の場合
        return false; // 非表示
    }
    return $visible; // その他の場合、デフォルトの可視性を維持
}

このコードは、製品属性 “体重” を非表示にする例です。

サンプル 2: ユーザーのロールに基づく可視性制御

add_filter('woocommerce_attribute_default_visibility', 'control_visibility_by_role', 10, 2);

function control_visibility_by_role($visible, $attribute) {
    if (current_user_can('subscriber') && $attribute->get_name() === 'サイズ') {
        return false; // 'サイズ'属性をサブスクライバーロールのユーザーには非表示
    }
    return $visible;
}

この例では、サブスクライバーロールのユーザーに対して “サイズ” 属性を非表示にします。

サンプル 3: 特定の条件で属性を非表示にする

add_filter('woocommerce_attribute_default_visibility', 'conditional_visibility', 10, 2);

function conditional_visibility($visible, $attribute) {
    if (is_product() && (float)WC()->cart->total < 50 && $attribute->get_name() === 'サービス') {
        return false; // 合計金額が50未満の場合に'サービス'属性を非表示
    }
    return $visible;
}

このコードは、カートの合計金額が50未満の場合に “サービス” 属性を非表示にします。

サンプル 4: 売切れ製品の属性を非表示にする

add_filter('woocommerce_attribute_default_visibility', 'hide_attribute_if_out_of_stock', 10, 2);

function hide_attribute_if_out_of_stock($visible, $attribute) {
    global $product;
    if (!$product->is_in_stock() && $attribute->get_name() === 'カラー') {
        return false; // 売切れの場合、'カラー'属性を非表示
    }
    return $visible;
}

このコードでは、製品が売切れの場合に “カラー” 属性を非表示にします。

サンプル 5: 管理者のみに属性を表示する

add_filter('woocommerce_attribute_default_visibility', 'show_attribute_for_admin', 10, 2);

function show_attribute_for_admin($visible, $attribute) {
    if (!current_user_can('administrator') && $attribute->get_name() === '特別属性') {
        return false; // 管理者以外には'特別属性'を非表示
    }
    return $visible;
}

このコードは、管理者以外のユーザーには “特別属性” を非表示にする例です。

これらのサンプルコードはすべて著作権フリーのものであり、WooCommerce のフィルタ機能を活用して製品属性の可視性をカスタマイズする方法を示しています。引用元はそれぞれのWooCommerceの公式ドキュメントやフォーラムをご参照ください。

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


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