我找到了一些代码(下面),我修改了这些代码,以便在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'];
}
}
答案 0 :(得分:7)
我相信您希望在_stock_status
函数中使用_stock
而不是parse_query
的meta_key,并将restrict_manage_posts
数组中的值更改为{{1 }和instock
。我在我的woocommerce商店测试了这个代码,过滤器适用于库存和缺货商品。
outofstock