【问题标题】:Woocommerce Admin Filter by In Stock / Out Of StockWoocommerce 管理员按库存/缺货过滤
【发布时间】:2016-10-31 08:12:06
【问题描述】:

我发现了一些代码(如下),我对其进行了修改,以便在 woo commerce 的产品页面上放置一个额外的过滤器,以按库存和缺货进行过滤。我可以缺货去工作,但就是不知道如何让它在库存中工作。我知道这与'In Stock' => '=

    <?php

add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
/**
 * First create the dropdown
 * make sure to change POST_TYPE to the name of your custom post type
 * 
 * @author Ohad Raz
 * 
 * @return void
 */
function wpse45436_admin_posts_filter_restrict_manage_posts(){
    $type = 'product';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }

    //only add filter to post type you want
    if ('product' == $type){
        //change this to the list of values you want to show
        //in 'label' => 'value' format
        $values = array(
            'Out of Stock' => '0', 
            'In Stock' => '=<1',
        );
        ?>
        <select name="StockLevel">
        <option value=""><?php _e('Filter By ', 'wose45436'); ?></option>
        <?php
            $current_v = isset($_GET['StockLevel'])? $_GET['StockLevel']:'';
            foreach ($values as $label => $value) {
                printf
                    (
                        '<option value="%s"%s>%s</option>',
                        $value,
                        $value == $current_v? ' selected="selected"':'',
                        $label
                    );
                }
        ?>
        </select>
        <?php
    }
}


add_filter( 'parse_query', 'wpse45436_posts_filter' );
/**
 * if submitted filter by post meta
 * 
 * make sure to change META_KEY to the actual meta key
 * and POST_TYPE to the name of your custom post type
 * @author Ohad Raz
 * @param  (wp_query object) $query
 * 
 * @return Void
 */
function wpse45436_posts_filter( $query ){
    global $pagenow;
    $type = 'product';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['StockLevel']) && $_GET['StockLevel'] != '') {
        $query->query_vars['meta_key'] = '_stock';
        $query->query_vars['meta_value'] = $_GET['StockLevel'];
    }
}

【问题讨论】:

    标签: wordpress filtering woocommerce


    【解决方案1】:

    我相信您会希望在您的parse_query 函数中使用_stock_status 的元键而不是_stock,并将restrict_manage_posts 数组中的值更改为instockoutofstock。我在我的 woocommerce 商店中测试了此代码,过滤器适用于 In Stock 和 Out of Stock 商品。

    <?php 
    /* Add In/Out of Stock Filter to Admin */
    add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
    
    function wpse45436_admin_posts_filter_restrict_manage_posts(){
    
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
    
        //only add filter to post type you want
        if ('product' == $type){
            //change this to the list of values you want to show
            //in 'label' => 'value' format
            $values = array(
                'Out of Stock' => 'outofstock', 
                'In Stock' => 'instock',
            );
            ?>
            <select name="Stock">
            <option value=""><?php _e('Show All Stock', 'wpse45436'); ?></option>
            <?php
                $current_v = isset($_GET['Stock'])? $_GET['Stock']:'';
                foreach ($values as $label => $value) {
                    printf
                        (
                            '<option value="%s"%s>%s</option>',
                            $value,
                            $value == $current_v? ' selected="selected"':'',
                            $label
                        );
                    }
            ?>
            </select>
            <?php
        }
    }
    
    
    add_filter( 'parse_query', 'wpse45436_posts_filter' );
    
    function wpse45436_posts_filter( $query ){
        global $pagenow;
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['Stock']) && $_GET['Stock'] != '') {
            $query->query_vars['meta_key'] = '_stock_status';
            $query->query_vars['meta_value'] = $_GET['Stock'];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 2015-11-16
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多