Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vyskoczilova/fbc9dd818af20ff514cdb2d50eab410a to your computer and use it in GitHub Desktop.
Save vyskoczilova/fbc9dd818af20ff514cdb2d50eab410a to your computer and use it in GitHub Desktop.
WooCommerce add stock filter to WordPress Admin (Products)
/**
* @snippet Add Stock Filter to Products page in WordPress admin | WooCommerce
* @comment Localization is automatic if you have localized WooCommerce to your language
* @source https://gist.github.com/vyskoczilova/fbc9dd818af20ff514cdb2d50eab410a
* @updatedversionof https://popoleodesigns.com/add-inout-of-stock-filter-to-wordpress-admin/
* @author Karolína Vyskočilová
* @testedwith WooCommerce 3.0.7
*/
// -------------------
add_action( 'restrict_manage_posts', 'my_theme_admin_posts_filter_restrict_manage_posts' );
function my_theme_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', 'woocommerce') => 'outofstock',
__('In stock', 'woocommerce') => 'instock',
);
?>
<select name="Stock">
<option value="">-- <?php _e('Stock', 'woocommerce'); ?> --</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', 'my_theme_posts_filter' );
function my_theme_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'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment