John Davidson

php - Display Woocommerce products selector only if number of products is more than 20

0 comments
Message:


Based on this (Change the displayed number of products in Woocommerce with Select dropdown) I use the following code to let the visitor display 20, 40 or 60 products on page:



//save and load the chosen option from session
function jc_get_products_per_page(){

global $woocommerce;

$default = 20;
$count = $default;
$options = jc_get_products_per_page_options();

// capture form data and store in session
if(isset($_POST['jc-woocommerce-products-per-page'])){
// set products per page from dropdown
$products_max = intval($_POST['jc-woocommerce-products-per-page']);
if($products_max != 0 && $products_max >= -1){
$woocommerce->session->jc_product_per_page = $products_max;
return $products_max;
}
}
// load product limit from session
if(isset($woocommerce->session->jc_product_per_page)){
// set products per page from woo session
$products_max = intval($woocommerce->session->jc_product_per_page);
if($products_max != 0 && $products_max >= -1){
return $products_max;
}
}
return $count;
}
add_filter('loop_shop_per_page','jc_get_products_per_page', 800);

//set the options for the dropdown
function jc_get_products_per_page_options(){
$options = apply_filters( 'jc_products_per_page', array(
20 => __('20', 'woocommerce'),
30 => __('30', 'woocommerce'),
60 => __('60', 'woocommerce')
));

return $options;
}
//display the dropdown on front-end
function jc_woocommerce_products_per_page(){

$options = jc_get_products_per_page_options();

$current_value = jc_get_products_per_page();
?>
<div id="products-per-page">
<div id="select_kaarten"><form action="" label="denny" method="POST" class="woocommerce-products-per-page">
<label></label>
<select name="jc-woocommerce-products-per-page" class="selectie" onchange="this.form.submit()">
<?php foreach($options as $value => $name): ?>
<option value="<?php echo $value; ?>" <?php selected($value, $current_value); ?>><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
</form></div>
</div>

<?php
}

add_action('woocommerce_before_shop_loop', 'jc_woocommerce_products_per_page', 100);


remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
// remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );

add_action( 'woocommerce_after_shop_loop', 'woocommerce_result_count', 8 );

This selector is shown on every category page, but I want to only run it on pages where the number of products is more than 20 products.


How can I achieve this?



My Answer:
&& $products_max <= 60){__ $count = $products_max;_ $_SESSION['jc-woocommerce-products-per-page'] = $count;_ }_ } elseif(isset($_SESSION['jc-woocommerce-products-per-page'])){_ $count = $_SESSION['jc-woocommerce-products-per-page'];_ }_ return $count;_}_add_filter('loop_shop_per_page', 'jc_get_products_per_page');__//display the dropdown on shop page_function jc_woocommerce_catalog_page_ordering(){__ $count = jc_get_products_per_page();__ echo '
';_ echo '';_ echo '';_ echo '
';}add_action('woocommerce_before_shop_loop', 'jc_woocommerce_catalog_page_ordering');__//define the options for the dropdown function jc_get_products_per_page_options(){__ return array(20, 40, 60);}?>

This code will display a dropdown selector on the WooCommerce shop page that allows the visitor to choose the number of products to display per page (20, 40, or 60). The selected option is saved in the session so that it persists across page loads. The function `jc_get_products_per_page` handles getting and setting the selected option, while `jc_woocommerce_catalog_page_ordering` displays the dropdown on the shop page. The `jc_get_products_per_page_options` function defines the options available in the dropdown.

Rate this post

3 of 5 based on 5852 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us