Chybí vám v administraci WooCommerce filtr podle toho, jestli je daný produkt skladem nebo není? Pokud je odpověď ano, tak právě dnešní snippet do functions.php je pro vás jako stvořený.
Postup je jednoduchý – vložte následující kód do functions.php své child šablony a nahrajte ho na svůj web. Pokud nevíte, jak na odvozenou šablonu, tak návod najdete v tomto příspěvku.
P. S. Kód pro filtr je plně lokalizovaný – automaticky by se vám měl zobrazit jazyk, který ve WooCommerce používáte.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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']; | |
} | |
} |