仅当产品数量超过 20 时才显示 Woocommerce 产品选择器

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

基于此()我使用下面的代码让访问者在页面上显示20、40或60个产品:


//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 );

此选择器显示在每个类别页面上,但我只想 运行 它显示在产品数量超过 20 个产品的页面上。

我怎样才能做到这一点?

这实际上非常简单,只需将输出放在 if {} 循环中以检查使用的产品数量 wc_get_loop_prop( 'total' ); 。在上面的代码中,我只需要在 wc_get_loop_prop( 'total' ) > 20 .

时输出 <div id="products-per-page">

完整的代码如下所示:

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" 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
} 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 );