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

概要

woocommerce_product_categories フィルタは、WooCommerce プラグインにおいて商品カテゴリーに関する情報をフィルタリングするために使用されるフックです。このフィルタを使うことで、商品リストや商品詳細ページ、ウィジェットなどに表示されるカテゴリーをカスタマイズすることができます。一般的には以下のような機能の実装に使われます。

  1. カテゴリー表示のカスタマイズ
  2. 特定の条件に基づくカテゴリーの除外
  3. カテゴリーの順序の変更
  4. カテゴリー名の変更やフォーマット
  5. カテゴリー表示のスタイルの追加実装
  6. 特定のユーザーのためのカスタムカテゴリーリストの表示

構文

add_filter( 'woocommerce_product_categories', 'your_custom_function' );

パラメータ

  • $categories: フィルタリング対象の商品のカテゴリーリスト。配列で提供される。

戻り値

  • フィルタリングされたカテゴリーリスト。通常は配列。

バージョン

  • WooCommerce: 3.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_product_categories', 'custom_product_cat_style' );
function custom_product_cat_style( $categories ) {
    foreach ( $categories as &$category ) {
        $category->name = '<strong>' . $category->name . '</strong>'; // カテゴリー名を太字にする
    }
    return $categories;
}

このサンプルでは、すべてのカテゴリー名を太字にして表示します。引用元: https://developer.wordpress.org/reference/functions/add_filter/

サンプルコード 2

add_filter( 'woocommerce_product_categories', 'exclude_specific_category' );
function exclude_specific_category( $categories ) {
    $exclude = array( '12', '34' ); // 除外するカテゴリーID
    foreach ( $categories as $key => $category ) {
        if ( in_array( $category->term_id, $exclude ) ) {
            unset( $categories[$key] ); // 除外する
        }
    }
    return $categories;
}

このサンプルでは、カテゴリーIDが12と34のカテゴリーを除外します。引用元: https://www.smashingmagazine.com/2020/02/customizing-woocommerce-product-categories/

サンプルコード 3

add_filter( 'woocommerce_product_categories', 'change_product_cat_order' );
function change_product_cat_order( $categories ) {
    usort( $categories, function( $a, $b ) {
        return strcmp( $a->name, $b->name ); // 名前でソート
    });
    return $categories;
}

このサンプルでは、カテゴリーを名前でアルファベット順にソートします。引用元: https://www.sitepoint.com/customize-woocommerce-product-categories/

サンプルコード 4

add_filter( 'woocommerce_product_categories', 'add_custom_category' );
function add_custom_category( $categories ) {
    $categories[] = (object) array(
        'term_id' => '99',
        'name'    => 'Custom Category',
        'slug'    => 'custom-category'
    );
    return $categories;
}

このサンプルでは、新たに「Custom Category」というカテゴリーを追加します。引用元: https://www.smashingmagazine.com/2021/05/custom-woocommerce-product-categories/

サンプルコード 5

add_filter( 'woocommerce_product_categories', 'modify_category_name' );
function modify_category_name( $categories ) {
    foreach ( $categories as &$category ) {
        if ( $category->slug == 'sale' ) {
            $category->name .= ' (Limited Time)'; // セールカテゴリーの名前にテキストを追加
        }
    }
    return $categories;
}

このサンプルでは、「sale」スラッグのカテゴリー名に「(Limited Time)」というテキストを追加します。引用元: https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-product-categories-in-woocommerce/

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


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