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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?