The problem is my code is working fine, it shows brand image and products form the same brand fo single product page , but it also shows products which have their catalog visibility hidden, can someone please help me with this , so my code doesn't show products which have catalog visibility hidden.
here's my code for the reference :-
`<div class="brand-section">
<?php
// Get the current product
global $product;
// Get the product brand term (replace 'product_brand' with your actual brand taxonomy)
$brand_term = wp_get_post_terms($product->get_id(), 'product_brand', array('fields' => 'ids'));
// Display the brand image
if (!empty($brand_term)) {
$brand_id = $brand_term[0];
$brand_image_id = get_term_meta($brand_id, 'brand_image', true);
if (!empty($brand_image_id)) {
echo '<div class="brand-image">';
echo '<img src="' . esc_url(wp_get_attachment_image_url($brand_image_id, 'full')) . '" alt="' . esc_attr(get_term($brand_id)->name) . '">';
echo '</div>';
}
}
?>
<?php`
// Display other products from the same brand
if (!empty($brand_term)) {
add_filter('woocommerce_product_query_tax_query', 'custom_exclude_hidden_products', 10, 2);
$args = array(
'post_type' => 'product',
'posts_per_page' => 4, // Show only 4 products
'tax_query' => array(
array(
'taxonomy' => 'product_brand',
'field' => 'id',
'terms' => $brand_term,
),
),
'post__not_in' => array($product->get_id()), // Exclude the current product
);
$brand_query = new WP_Query($args);
remove_filter('woocommerce_product_query_tax_query', 'custom_exclude_hidden_products', 10);
if ($brand_query->have_posts()) {
echo '<div class="other-products-from-brand">';
echo '<h3 class="other-products-heading">Other Products from ' . esc_html(get_term($brand_id)->name) . '</h3>';
while ($brand_query->have_posts()) {
$brand_query->the_post();
// Display other product information (e.g., product image, title, price, add to cart button)
echo '<div class="product-item">';
echo '<a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a>';
echo '<h4><a href="' . get_permalink() . '">' . get_the_title() . '</a></h4>';
echo '<div class="price">' . wc_price(get_post_meta(get_the_ID(), '_regular_price', true)) . '</div>';
echo '<div class="add-to-cart-button">' . do_shortcode('[add_to_cart id="' . get_the_ID() . '"]') . '</div>';
echo '</div>';
}
echo '</div>';
wp_reset_postdata();
}
}
// Custom function to exclude hidden products from the query
function custom_exclude_hidden_products($tax_query, $query) {
if ($query->is_main_query() && is_singular('product')) {
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('exclude-from-catalog', 'exclude-from-search', 'outofstock'),
'operator' => 'NOT IN',
);
}
return $tax_query;
}
?>
</div>`
generally the products that are grouped within a different product and have catalog visibility hidden are getting shown as output
I tried using different visibility taxonomies but nothing works, the code should output products which only have visibility set to catalog & search & not display products with catalog visibility hidden