Archived topic
Separate settings for Shop Page and Search Results
4 replies · Started by Filippo on April 14, 2020
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!
I will add that commenting the function for displaying featured products in the Shop Page make things work as expected again.
Hi there,
Just to confirm, the search settings filter works fine without the woocommerce_product_query_tax_query filter?
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.
Yea, that alters the WooCommerce query itself. If possible, it's best to leave it as is :)