I have almost zero knowledge with Wordpress and Wocoommerce cores and api's so, please bear with me.
I have showing ~140 products on custom page (e.g. not the default shop page for woocommerce) trough WooCommerce Product Filter
.
The products are on page and everything is fine except there is no way to show dropdown Products per page
which I can choose how many products to show on page.
So, I've found some tutorials and currently I have this in my function.php
add_action( 'woocommerce_before_shop_loop', 'ps_selectbox', 25 );
function ps_selectbox() {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
echo '<span>Per Page: </span>';
echo '<select onchange="if (this.value) window.location.href=this.value">';
$orderby_options = array( '8' => '8', '16' => '16','32' => '32','64' => '64' );
foreach( $orderby_options as $value => $label ) {
echo "<option ".selected( $per_page, $value )." value='?perpage=$value'>$label</option>";
}
echo '</select>';
}
add_action( 'pre_get_posts', 'ps_pre_get_products_query' );
function ps_pre_get_products_query( $query ) {
$per_page = filter_input(INPUT_GET, 'perpage', FILTER_SANITIZE_NUMBER_INT);
if( $query->is_main_query() && !is_admin() && is_page() ) {
$query->set( 'posts_per_page', $per_page );
}
}
function render_ps_sel() {
return ps_selectbox();
}
add_shortcode( 'ps_selectbox', 'render_ps_sel' );
I'm render the dropdown menu trough the shortcode above [ps_selectbox]
but whatever I choose the number of the products on the page is always same and nothing change.
$per_page
as the second parameter forfilter_input
isperpagee
and on row 14 it's justperpage
.