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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。