Woocommerce Admin按库存/缺货过滤

use*_*291 4 wordpress filtering woocommerce

我发现了一些代码(下面),我修改了这些代码,以便在woo commerce的产品页面上添加一个额外的过滤器,以便按库存和缺货过滤.我可以脱机工作,但只是无法弄清楚如何让它在库存中工作..我知道这与这条线'In Stock'=>'= <1'有关,但无法弄清楚是什么它应该是.非常感谢帮助

    <?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'];
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

我相信你会想要使用meta_key _stock_status而不是_stock你的parse_query函数,并将restrict_manage_posts数组中的值更改为instockoutofstock.我在我的woocommerce商店测试了这个代码,过滤器适用于库存和缺货商品.

<?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'];
    }
}
Run Code Online (Sandbox Code Playgroud)