[Support request] Separate settings for Shop Page and Search Results

Home Forums Support Separate settings for Shop Page and Search Results

  • This topic has 4 replies, 2 voices, and was last updated 12 months ago by Tom.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1238342
    Filippo

    Hi there, I’m using the following functions for my Woocommerce Shop Page:

    /* ---------- Woocommerce Shop Page - Categories on Columns ---------- */
    
    add_filter( 'option_generate_woocommerce_settings','lh_custom_category_wc_columns' );
    function lh_custom_category_wc_columns( $options ) {
        if ( is_shop() ) {
            $options['columns'] = 1;
    		$options['product_sorting'] = false;
    		$options['product_results_count'] = false;
    		$options['products_per_page'] = 6;
        }
        
        return $options;
    }
    
    /* ---------- Display Featured Products ---------- */
    
    add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
    function custom_product_query_tax_query( $tax_query, $query ) {
        if( is_admin() ) return $tax_query;
        if ( is_shop() ) {
            $tax_query[] = array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => 'featured'
            );
        }
    	
        return $tax_query;
    }

    And the following functions for my Search Results (basically I do want them to be in one column layout, and to be Products only, no Posts):

    /* ---------- Search Layout (one column) ---------- */
    
    add_filter( 'option_generate_woocommerce_settings','search_custom_wc_columns' );
    function search_custom_wc_columns( $options ) {
        if ( is_search() ) {
            $options['columns'] = 1;
    		$options['product_sorting'] = true;
    		$options['product_results_count'] = true;
    		$options['products_per_page'] = 12;
        }
        
        return $options;
    }
    
    /* ---------- Search Placeholder (products only) ---------- */
    
    add_filter( 'generate_navigation_search_output', function() {
        printf(
            '<form method="get" class="search-form navigation-search" action="%1$s">
                <input type="search" placeholder="Search by title, genre, instrument, etc." class="search-field" value="%2$s" name="s" title="%3$s" />
    			<input type="hidden" name="post_type" value="product" />
            </form>',
            esc_url( home_url( '/' ) ),
            esc_attr( get_search_query() ),
            esc_attr_x( 'Search', 'label', 'generatepress' ) 
        ); 
    } );

    The problem is that everything I added for the Search Results is ignored, and the functions I’m using for the Shop Page are working instead. Can you help with that? Not sure if this is something related to Generatepress or just Woocommerce.

    Thanks in advance!

    #1238348
    Filippo

    I will add that commenting the function for displaying featured products in the Shop Page make things work as expected again.

    #1238966
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Just to confirm, the search settings filter works fine without the woocommerce_product_query_tax_query filter?

    #1239724
    Filippo

    Thanks for your reply, Tom.

    Yes, everything works normally when I remove that “Display Featured Products” function. I wanted to have only my featured products in the main Shop Page, while using default settings in all the other pages (and in the search results page too, of course).

    Now I think it has nothing to do with GeneratePress, I think I will just remove the function for now.

    #1240528
    Tom
    Lead Developer
    Lead Developer

    Yea, that alters the WooCommerce query itself. If possible, it’s best to leave it as is 🙂

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