Home > Software design >  Display Woocommerce products selector only if number of products is more than 20
Display Woocommerce products selector only if number of products is more than 20

Time:05-05

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" >
        <label></label>
            <select name="jc-woocommerce-products-per-page"  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?

CodePudding user response:

This was actually quiet simple, just had to put the output in a if {} loop to check the number of products using wc_get_loop_prop( 'total' ); . In the above code, I needed to output <div id="products-per-page"> only if wc_get_loop_prop( 'total' ) > 20 .

The complete code looks like this:

function jc_get_products_per_page(){

    global $woocommerce;

    $default = 30;
    $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();
    ?>
  

<?php 
    // SHOW ONLY IF MORE THAN 20 PRODUCTS  
$aantalpr = wc_get_loop_prop( 'total' ); 
if ($aantalpr > 20) {  ?>
<div id="products-per-page">
    <div id="select_kaarten"><form action="" label="denny" method="POST" >
        <label></label>
            <select name="jc-woocommerce-products-per-page"  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
} else {
echo "";
}
}
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 );
// add_action( 'woocommerce_after_shop_loop', 'woocommerce_catalog_ordering', 9 );
  • Related