Site logo

[Resolved] Deactivate Loop Template for WooCommerce Product Search

Home Forums Support [Resolved] Deactivate Loop Template for WooCommerce Product Search

Home Forums Support Deactivate Loop Template for WooCommerce Product Search

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2566446
    René

    Hello, how can I disable the loop template for the WooCommerce product search (WooCommerce Block)? For the normal search, however, the template should remain.

    #2566653
    Fernando
    Customer Support

    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

    #2567716
    René

    Thank you, but this does not work :/

    #2567728
    Ying
    Staff
    Customer Support

    Can you link us to a Woo product search result page?

    #2567729
    René

    Hey, yes. Check the private information

    I already changed the “is_search” to “is_search()”.

    #2567731
    René

    Deleted

    #2567848
    Ying
    Staff
    Customer Support

    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?

    #2568150
    René

    the product search results are influenced by the loop template. If I disable the loop template, the search results are displayed like products.

    #2568397
    David
    Staff
    Customer Support

    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 );
    #2568477
    René

    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;
                }
    
            }
        }
    #2568515
    David
    Staff
    Customer Support

    Glad to hear you got a working solution.

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.