概要
woocommerce_product_supports
フィルタは、WooCommerceにおける特定の商品の機能やサポートをカスタマイズするために使用されます。このフィルタは、商品タイプに応じて特定の支持機能(サポート機能)を追加または削除する際に便利です。具体的には、商品のカスタムフィールドやメタデータ、商品バリエーション、特定の設定項目などに関連する機能を調整できます。
よく使用される機能としては以下のようなものがあります。
1. 商品のギャラリー機能の有無
2. 商品のバリエーション管理
3. 特定のカスタムフィールドの表示/非表示
4. 商品のレビュー機能の有無
5. 在庫管理機能の追加
6. ダウンロード商品の特性
このフィルタは、WooCommerceバージョン3.0以降で使用可能で、WordPressバージョン4.0以降に対応しています。
構文
add_filter( 'woocommerce_product_supports', 'my_custom_supports', 10, 2 );
パラメータ
supports
(array): 商品がサポートしている機能のリストproduct
(WC_Product): 対象の商品オブジェクト
戻り値
- (array): サポートしている機能の更新されたリスト
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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_product_supports', 'disable_reviews_support', 10, 2 );
function disable_reviews_support( $supports, $product ) {
if ( 'simple' === $product->get_type() ) {
$supports[] = 'reviews';
}
return $supports;
}
このコードは、シンプル型商品のレビュー機能を無効化します。
サンプル2: 在庫管理機能の追加
add_filter( 'woocommerce_product_supports', 'add_stock_management', 10, 2 );
function add_stock_management( $supports, $product ) {
if ( 'variable' === $product->get_type() ) {
$supports[] = 'stock';
}
return $supports;
}
このコードは、バリエーション型商品の在庫管理機能を追加します。
サンプル3: ギャラリー機能の有効化
add_filter( 'woocommerce_product_supports', 'enable_gallery_support', 10, 2 );
function enable_gallery_support( $supports, $product ) {
if ( 'subscription' === $product->get_type() ) {
$supports[] = 'gallery';
}
return $supports;
}
このコードは、サブスクリプション型商品にギャラリー機能を追加します。
サンプル4: カスタムフィールドのサポート
add_filter( 'woocommerce_product_supports', 'add_custom_field_support', 10, 2 );
function add_custom_field_support( $supports, $product ) {
$supports[] = 'custom_fields';
return $supports;
}
このコードは、全ての商品にカスタムフィールドのサポートを追加します。
サンプル5: ダウンロード商品の特殊機能を有効化
add_filter( 'woocommerce_product_supports', 'enable_downloadable_support', 10, 2 );
function enable_downloadable_support( $supports, $product ) {
if ( $product->is_downloadable() ) {
$supports[] = 'downloads';
}
return $supports;
}
このコードは、ダウンロード可能な商品に特有の機能を有効にします。
(著作権フリーのサンプルコードは一般的に提供されていますが、具体的な引用元のページは参照していません。)