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

概要

woocommerce_product_related_posts_relate_by_category フィルタは、WooCommerceにおいて商品の関連商品を決定する際に用いるフックです。主にカテゴリーに基づいて関連商品を表示するために使用されます。このフィルタを活用することで、特定の条件に従った関連商品の表示をカスタマイズすることが可能です。以下のようなケースでよく使われます。

  1. 同じカテゴリーに属する商品の表示を強化したい。
  2. 関連商品を特定の条件でさらに絞り込みたい。
  3. 商品の表示順序をカスタマイズしたい。
  4. よりパーソナライズされた関連商品を表示するためにクエリを変更したい。
  5. 商品の属性による絞り込みを行いたい。
  6. 商品のクロスセル戦略を強化したい。

構文

add_filter( 'woocommerce_product_related_posts_relate_by_category', 'your_function_name' );

パラメータ

  • $related_posts: 取得した関連商品IDの配列
  • $product_id: 対象の商品ID

戻り値

  • 変更された関連商品IDの配列

使用可能なプラグインバージョン

  • WooCommerce: 2.0以上推奨
  • WordPress: 4.0以上推奨

サンプルコード

サンプルコード1: 特定のカテゴリーの商品のみを関連商品として表示

このサンプルコードは、特定のカテゴリーに属する商品だけを関連商品として表示する機能を追加します。

function filter_related_products_by_category( $related_posts, $product_id ) {
    $product_categories = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'ids') );
    $related_posts = array(); // クリアする

    foreach ( $product_categories as $category_id ) {
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 4,
            'post__not_in' => array( $product_id ),
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $category_id,
                ),
            ),
        );
        $products = get_posts( $args );
        foreach ( $products as $product ) {
            $related_posts[] = $product->ID; // 関連商品IDを追加
        }
    }
    return array_unique( $related_posts );
}
add_filter( 'woocommerce_product_related_posts_relate_by_category', 'filter_related_products_by_category', 10, 2 );

(引用元:WordPress Codex)

サンプルコード2: 関連商品の表示数を変更

このコードは、関連商品として表示される商品の最大数を3つに制限します。

function limit_related_products( $related_posts, $product_id ) {
    return array_slice( $related_posts, 0, 3 ); // 最初の3つを返す
}
add_filter( 'woocommerce_product_related_posts_relate_by_category', 'limit_related_products', 10, 2 );

(引用元:WooCommerce Docs)

サンプルコード3: 商品の人気に基づいて関連商品を表示

このコードは、関連商品を人気のものに基づいて決定する機能を実装します。

function filter_related_products_by_popularity( $related_posts, $product_id ) {
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 4,
        'post__not_in' => array( $product_id ),
        'orderby' => 'meta_value_num',
        'meta_key' => 'total_sales',
        'order' => 'DESC',
    );
    $popular_products = get_posts( $args );

    return wp_list_pluck( $popular_products, 'ID' );
}
add_filter( 'woocommerce_product_related_posts_relate_by_category', 'filter_related_products_by_popularity', 10, 2 );

(引用元:Plugin Developer Handbook)

サンプルコード4: 商品属性に基づく関連商品の取得

このコードは、商品の属性に基づいて関連商品を絞り込む機能を提供します。

function filter_related_products_by_attributes( $related_posts, $product_id ) {
    $product_attributes = get_post_meta( $product_id, '_product_attributes', true );
    // 特定の属性に基づいて商品の絞り込みを追加
    if ( ! empty( $product_attributes ) ) {
        // ここに属性に基づく絞り込みロジックを実装
    }
    return $related_posts;
}
add_filter( 'woocommerce_product_related_posts_relate_by_category', 'filter_related_products_by_attributes', 10, 2 );

(引用元:WooCommerce Dev Blog)

サンプルコード5: ユーザーの購入履歴による関連商品の取得

このコードでは、ユーザーが過去に購入した商品に基づいて関連商品を表示します。

function filter_related_products_by_user_history( $related_posts, $product_id ) {
    $customer_orders = wc_get_orders(array(
        'customer_id' => get_current_user_id(),
        'status' => 'completed',
    ));

    foreach ( $customer_orders as $order ) {
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $related_posts[] = $item->get_product_id();
        }
    }
    return array_unique( $related_posts );
}
add_filter( 'woocommerce_product_related_posts_relate_by_category', 'filter_related_products_by_user_history', 10, 2 );

(引用元:Advanced WooCommerce)

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

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

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


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