【发布时间】:2016-10-31 08:12:06
【问题描述】:
我发现了一些代码(如下),我对其进行了修改,以便在 woo commerce 的产品页面上放置一个额外的过滤器,以按库存和缺货进行过滤。我可以缺货去工作,但就是不知道如何让它在库存中工作。我知道这与'In Stock' => '=
<?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'];
}
}
【问题讨论】:
标签: wordpress filtering woocommerce