Sample to add filters on related products
function filter_related_products_exclude_attribute_value( $related_product_ids, $product_id ) {
$filtered_related_product_ids = array();
foreach ( $related_product_ids as $related_product_id ) {
$related_product = wc_get_product( $related_product_id );
$attribute_value = $related_product->get_attribute( 'attribute_name_here' );
if ( $attribute_value !== 'N/A' ) {
$filtered_related_product_ids[] = $related_product_id;
}
}
return $filtered_related_product_ids;
}
add_filter( 'woocommerce_related_products', 'filter_related_products_exclude_attribute_value', 10, 2 );
function filter_related_products_exclude_custom_field_value( $related_product_ids, $product_id ) {
$filtered_related_product_ids = array();
foreach ( $related_product_ids as $related_product_id ) {
$custom_field_value = get_post_meta( $related_product_id, 'custom_field_name_here', true );
if ( $custom_field_value !== 'N/A' ) {
$filtered_related_product_ids[] = $related_product_id;
}
}
return $filtered_related_product_ids;
}
add_filter( 'woocommerce_related_products', 'filter_related_products_exclude_custom_field_value', 10, 2 );
Using custom page with query
$related_products = get_posts(
array(
'category__in' => wp_get_post_categories( $post->ID ),
'posts_per_page' => 4,
'post__not_in' => array( $post->ID ),
'post_type' => 'product',
'orderby' => 'rand ',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'attribute_one_here',
'field' => 'slug',
'terms' => 'the condition here',
],
[
'taxonomy' => 'attribute_second_here',
'field' => 'slug',
'terms' => 'the condition here',
],
[
'taxonomy' => 'attribute_third_here',
'field' => 'slug',
'terms' => 'the condition here',
]
],
'meta_query' => array(
array(
'key' => 'custom-field-to-filter', // Replace with the name of your custom field
'compare' => '!=', // Comparison operator (use '=', '!=', '>', '<', etc.)
'value' => 'N/A', // The value you want to filter by
),
)
)
);