概要
woocommerce_product_related_posts_force_display
フィルタは、WooCommerce の関連商品を表示する際の動作をカスタマイズするためのフックです。このフィルタは、特に新たな関連商品のロジックを追加したり、特定の条件下で関連商品を強制的に表示したりする場合に便利です。以下は、このフィルタを使って実装する際に考慮されることが多い機能の例です。
- 特定のカテゴリに属する商品のみを関連商品として表示
- 在庫のある商品のみを関連商品として表示
- 関連商品の表示順序をカスタマイズ
- 特定のプロダクト ID を持つ商品を優先的に表示
- 外部商品を関連商品として扱う
- ユーザーが購入した商品に基づく関連商品の推奨
構文
add_filter('woocommerce_product_related_posts_force_display', 'custom_function_name', 10, 2);
パラメータ
$related_posts
: 関連商品の ID の配列。デフォルトの関連商品を含む。$product_id
: 現在表示されている商品の ID。
戻り値
- フィルタリングされた関連商品の ID の配列。
使用可能なプラグインWooCommerceのバージョン
- WooCommerce バージョン 3.0 以降
WordPress のバージョン
- WordPress バージョン 4.5 以降
この関数のアクションでの使用可能性
アクション名 | 使用可能性 |
---|---|
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_related_posts_force_display', 'custom_related_products_by_category', 10, 2);
function custom_related_products_by_category($related_posts, $product_id) {
$product_terms = get_the_terms($product_id, 'product_cat');
if ($product_terms) {
foreach ($product_terms as $term) {
$related_posts = array_merge($related_posts, wc_get_products(['category' => [$term->slug], 'exclude' => [$product_id], 'limit' => 4]));
}
}
return array_unique($related_posts);
}
このサンプルコードは、現在の商品のカテゴリに基づいて関連商品を取得します。
引用元: https://woocommerce.com/
サンプル 2: 在庫商品のみを表示
add_filter('woocommerce_product_related_posts_force_display', 'custom_related_products_in_stock', 10, 2);
function custom_related_products_in_stock($related_posts, $product_id) {
return array_filter($related_posts, function($post_id) {
return wc_get_product($post_id)->is_in_stock();
});
}
このサンプルコードは、関連商品リストから在庫がある商品のみを返します。
引用元: https://woocommerce.com/
サンプル 3: 特定のプロダクト ID を優先的に表示
add_filter('woocommerce_product_related_posts_force_display', 'custom_preferred_related_product', 10, 2);
function custom_preferred_related_product($related_posts, $product_id) {
$preferred_product_id = 123; // 優先するプロダクトID
if (!in_array($preferred_product_id, $related_posts)) {
$related_posts[] = $preferred_product_id;
}
return $related_posts;
}
このサンプルコードは、特定のプロダクト ID を関係する商品リストに追加します。
引用元: https://woocommerce.com/
サンプル 4: 外部商品を表示
add_filter('woocommerce_product_related_posts_force_display', 'custom_include_external_products', 10, 2);
function custom_include_external_products($related_posts, $product_id) {
// 外部商品の条件を設定
$external_product_id = 456; // 外部商品のID
if (!in_array($external_product_id, $related_posts)) {
$related_posts[] = $external_product_id;
}
return $related_posts;
}
このサンプルコードは、外部商品を関連商品リストに追加します。
引用元: https://woocommerce.com/
サンプル 5: 関連商品の表示順序をカスタマイズ
add_filter('woocommerce_product_related_posts_force_display', 'custom_sort_related_posts', 10, 2);
function custom_sort_related_posts($related_posts, $product_id) {
// 任意のソートロジック
usort($related_posts, function($a, $b) {
return $a - $b; // ID の昇順でソート
});
return $related_posts;
}
このサンプルコードは、関連商品 ID を昇順でソートして返します。
引用元: https://woocommerce.com/