dqask02082 2017-10-09 17:37
浏览 111
已采纳

使用选择下拉列表更改Woocommerce中显示的产品数量

Trying to add a drop-down on products listing to change the displayed number of products. I am working on the latest version of Wordpress (4.8.2) and Woocommerce (3.1.2).

This is my code on functions.php

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

    global $woocommerce;

    $default = 5;
    $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');

//set the options for the dropdown
function jc_get_products_per_page_options(){
    $options = apply_filters( 'jc_products_per_page', array(
        5 => __('5', 'woocommerce'),
        10 => __('10', 'woocommerce'),
        15 => __('15', 'woocommerce'),
        20 => __('20', '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 class="products-per-page">
        <span>View:</span>
        <form action="" method="POST" class="woocommerce-products-per-page">
            <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>
    <?php
}

add_action('woocommerce_after_shop_loop', 'jc_woocommerce_products_per_page', 1);

So the problem is that when I change the dropdown, the page reload but the number of displayed products stay the same. The default. Any idea what is going wrong?

Edit: I tried to add a return 2; on the first row of the function and still doesn't work. So, it seems the add_filter doesn't work

  • 写回答

3条回答 默认 最新

  • dousi6303 2017-10-24 16:54
    关注

    I grabbed your code as is from your initial post and dropped it into a theme I'm developing for WooCommerce and it worked like a charm.

    Obviously, that's not your case. But this leads me to believe that something else is likely also hooking into the loop_shop_per_page filter after your code is being called.

    Before refactoring all of your code try setting the priority on your add_filter to something high, like so:

    add_filter('loop_shop_per_page','jc_get_products_per_page', 99);

    That should help to determine if it is an issue with your filter being overwritten by the same filter being called in another plugin or elsewhere in your theme.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀