Add In/Out of Stock Filter to WordPress Admin
Here is a custom WordPress/Woocommerce Function that adds a In/Out of Stock Filter to WordPress Admin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?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']; } } |
Comments (5)
Hi Brett,
Can you please tell me in which file should I paste this code?
I’m new at wordpress/woocommerce world
thanks!
The code would go into your functions.php file in the active theme folder. I have moved on from this approach, there is a free plugin that does the same thing. Check out //wordpress.org/plugins/woocommerce-sort-by-stock/.
This was a great help… Thanks!
Hello Friend..How can I reposition the “Out of Stock” label just below the product description on single product page??
Hey ,
I want to hide my stock for customers who do not have an account, how to do it ?
Thanks