gpt4 book ai didi

wordpress - Woocommerce Admin 按库存/缺货过滤

转载 作者:行者123 更新时间:2023-12-04 23:50:36 25 4
gpt4 key购买 nike

我找到了一些代码(如下),我修改了这些代码,在 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'];
}
}

最佳答案

我相信你会想要使用 _stock_status 的 meta_key而不是 _stock在您的 parse_query函数,并更改 restrict_manage_posts 中的值数组到 instockoutofstock .我在我的 woocommerce 商店中测试了此代码,该过滤器适用于库存和缺货商品。

<?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'];
}
}

关于wordpress - Woocommerce Admin 按库存/缺货过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com