Archived topic
Deactivate Loop Template for WooCommerce Product Search
10 replies · Started by René on March 13, 2023
Hello, how can I disable the loop template for the WooCommerce product search (WooCommerce Block)? For the normal search, however, the template should remain.
Hi Rene,
Can you try adding this snippet?:
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 100 === $element_id && is_search && is_woocommerce() ) {
$display = false;
}
return $display;
}, 10, 2 );
Replace 100 with the Loop Element's ID.
Getting the Element ID: https://share.getcloudapp.com/YEuDdrnQ
Thank you, but this does not work :/
Can you link us to a Woo product search result page?
Hey, yes. Check the private information
I already changed the "is_search" to "is_search()".
Deleted
You have a query loop added in the product search before the breadcrumbs, but the product search result itself is not using a loop template.
So which part are you referring to as the loop template in your question?
the product search results are influenced by the loop template. If I disable the loop template, the search results are displayed like products.
Hi there,
the Loop Template you have, what are the display rules? As you need to exclude the product archive from those.
if that causes a different issue then try this snippet where we check the search is showing only the product archive.
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 100 === $element_id && is_search() && is_post_type_archive( 'product' ) ) {
$display = false;
}
return $display;
}, 10, 2 );
Our programmer looked at this and solved it as follows. I'm sure I'm not the only one with this problem. Maybe an idea for one of the next updates.
Replace 1691 with your Element ID (Loop Template)
add_filter( 'generate_element_display', function( $display, $element_id ) {
if($display){
if(is_search()){
if ( ( get_post_type() === 'product') && (1691 === $element_id)) {
return false;
}
}
}
return $display;
}, 10, 2 );
add_action('pre_get_posts', 'remove_product_if_not_in_url');
function remove_product_if_not_in_url($query) {
if (is_admin() || !$query->is_main_query() || !$query->is_search()) {
return $query;
}
if($query->is_search()){
// Test if the query exists at the URL
if ( get_query_var('post_type') && get_query_var('post_type') == 'product' ) {
return $query;
}
else {
$cpt = [
'post',
'page'
];
$query->set('post_type', $cpt);
return $query;
}
}
}
Glad to hear you got a working solution.