Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Admin Filter by In Stock / Out Of Stock

I found some code (below) which I modified to put an extra filter on the products page in woo commerce to filter by in stock and out of stock. I can get out of stock to work, but just cannot figure out how to make it work by in stock.. I know it has to do with this line 'In Stock' => '=<1', but cannot figure out what it is meant to be. Help is greatly appreciated

    <?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'];
    }
}
like image 888
user3436291 Avatar asked Apr 09 '14 20:04

user3436291


1 Answers

I believe you will want to use the meta_key of _stock_status rather than _stock in your parse_query function, and change the values in the restrict_manage_posts array to instock and outofstock. I tested this code in my woocommerce store and the filter works for both In Stock and Out of Stock items.

<?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'];
    }
}
like image 62
designgirl Avatar answered Nov 02 '22 12:11

designgirl