Php WooCommerce-如何在没有';库存';产品

Php WooCommerce-如何在没有';库存';产品,php,wordpress,woocommerce,widget,taxonomy-terms,Php,Wordpress,Woocommerce,Widget,Taxonomy Terms,我有一个WooCommerce网站,当产品标签存档页面有0个“库存”产品可用时,需要在产品标签云中隐藏产品标签 下面的代码显示了我当前的进度,我会随着进度的提高而更新我相信这段代码可以用来运行检查并删除“show count”值小于1的任何结果,这将回答这个问题。此外,隐藏的结果需要302重定向到顶级“商店”页面,同时暂时隐藏: /* TURN PRODUCT TAG CLOUD INTO ALPHABETICAL LIST WITH TAG TOTALS COUNT VISIBLE */ f

我有一个WooCommerce网站,当产品标签存档页面有0个“库存”产品可用时,需要在产品标签云中隐藏产品标签

下面的代码显示了我当前的进度,我会随着进度的提高而更新我相信这段代码可以用来运行检查并删除“show count”值小于1的任何结果,这将回答这个问题。此外,隐藏的结果需要302重定向到顶级“商店”页面,同时暂时隐藏:

/* TURN PRODUCT TAG CLOUD INTO ALPHABETICAL LIST WITH TAG TOTALS COUNT VISIBLE */

function woocommerce_product_tag_cloud_widget_filter($args) {
    $args = array(
        'smallest' => 14, 
        'largest' => 14, 
        'format' => 'list', 
        'taxonomy' => 'product_tag', 
        'unit' => 'px',
        'show_count' => 1,
        'number' => 0,
    );

    echo "<div style='padding-left: 20px;'>";
    return $args;
    echo "</div>";
}

add_filter('woocommerce_product_tag_cloud_widget_args', 'woocommerce_product_tag_cloud_widget_filter');
此代码隐藏具有0“库存”产品的所有类别

add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
    global $wpdb;
    $nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
    foreach ( $items as $key => $item ) {
        if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
            unset( $items[$key] );
        }
    }
    return $items;
}

我尝试将上述代码的各个元素组合在一起,以创建一个实现我目标的函数,但是我的能力不足意味着我没有任何东西可以正常工作,因此我需要额外的帮助。

我已经设法拼凑出了以下解决这个问题的方法,实际上它又向前推进了几步,并添加了一些我试图在注释代码中解释的其他很酷的功能

如果任何人对此代码有任何反馈、补充、评论或问题,请联系!我将尝试扩展这段代码,这样它也可以作为一个细化功能,只显示与网站内用户位置相关的标签,并从这些级别删除所有不相关的标签-欢迎任何建议

特别感谢Facebook上的Md.Mehedi Hasan,他在代码的中间部分帮助隐藏了前端的产品标签归档页面

/* TURN PRODUCT TAG CLOUD INTO ALPHABETICAL LIST WITH TAG TOTALS COUNT VISIBLE */

function woocommerce_product_tag_cloud_widget_filter($args) {
    $args = array(
        'smallest' => 14, 
        'largest' => 14, 
        'format' => 'list', 
        'taxonomy' => 'product_tag', 
        'unit' => 'px',
        'show_count' => 1,
        'number' => 0,
    );

    echo "<div style='padding-left: 20px; min-height: 50px; max-height: 400px; overflow: auto;'>";
    return $args;
    echo "</div>";
}

add_filter('woocommerce_product_tag_cloud_widget_args', 'woocommerce_product_tag_cloud_widget_filter');

/* HIDE PRODUCT TAG ARCHIVE PAGES WHEN THEY HAVE NO 'IN STOCK' PRODUCTS */

function hide_empty_tags( $terms, $taxonomies) {
    $new_terms = array();
    
    if ( in_array( 'product_tag', $taxonomies ) && ! is_admin() ) {
        foreach ( $terms as $key => $term ) {
            if ($term->count >0){
                $new_terms[] = $term;
            }
        }
        $terms = $new_terms;
    }
    return $terms;
}

add_filter( 'get_terms', 'hide_empty_tags', 10, 3 );

/* REDIRECTS TO SHOP IF THERE ARE NO 'IN STOCK' PRODUCTS IN THE PRODUCT TAG ARCHIVE PAGE */

function redirect_to_shop(){
    global $wp_query;

    if( is_woocommerce() && $wp_query->post_count == 0 ){
        the_post();
    $post_url = "/shop";
    wp_safe_redirect($post_url , 302 );
    exit;
    }
} 

add_action('template_redirect', 'redirect_to_shop');
/*将产品标签云转换为字母列表,标签总数可见*/
功能商业\产品\标签\云\小部件\过滤器($args){
$args=数组(
“最小”=>14,
“最大”=>14,
'格式'=>'列表',
“分类法”=>“产品标签”,
'单位'=>'像素',
“显示计数”=>1,
“数字”=>0,
);
回声“;
返回$args;
回声“;
}
添加过滤器(“woocommerce_产品_标签_云_小部件_参数”,“woocommerce_产品_标签_云_小部件_过滤器”);
/*当没有“库存”产品时,隐藏产品标签存档页*/
函数隐藏\u空\u标记($terms,$taxonomies){
$new_terms=array();
if(在_数组('product_tag',$taxonomies)&&!is_admin()中){
foreach($key=>$term的术语){
如果($term->count>0){
$new_terms[]=$term;
}
}
$terms=$new_术语;
}
返回$terms;
}
添加过滤器('get_terms','hide_empty_tags',10,3);
/*如果产品标签存档页面中没有“库存”产品,则重定向到商店*/
函数将_重定向到_shop(){
全局$wp_查询;
如果(is_woocommerce()&&$wp_query->post_count==0){
_post();
$post_url=“/shop”;
wp_安全重定向($post_url,302);
出口
}
} 
添加操作(“模板重定向”,“重定向到车间”);
add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
    global $wpdb;
    $nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
    foreach ( $items as $key => $item ) {
        if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
            unset( $items[$key] );
        }
    }
    return $items;
}
/* TURN PRODUCT TAG CLOUD INTO ALPHABETICAL LIST WITH TAG TOTALS COUNT VISIBLE */

function woocommerce_product_tag_cloud_widget_filter($args) {
    $args = array(
        'smallest' => 14, 
        'largest' => 14, 
        'format' => 'list', 
        'taxonomy' => 'product_tag', 
        'unit' => 'px',
        'show_count' => 1,
        'number' => 0,
    );

    echo "<div style='padding-left: 20px; min-height: 50px; max-height: 400px; overflow: auto;'>";
    return $args;
    echo "</div>";
}

add_filter('woocommerce_product_tag_cloud_widget_args', 'woocommerce_product_tag_cloud_widget_filter');

/* HIDE PRODUCT TAG ARCHIVE PAGES WHEN THEY HAVE NO 'IN STOCK' PRODUCTS */

function hide_empty_tags( $terms, $taxonomies) {
    $new_terms = array();
    
    if ( in_array( 'product_tag', $taxonomies ) && ! is_admin() ) {
        foreach ( $terms as $key => $term ) {
            if ($term->count >0){
                $new_terms[] = $term;
            }
        }
        $terms = $new_terms;
    }
    return $terms;
}

add_filter( 'get_terms', 'hide_empty_tags', 10, 3 );

/* REDIRECTS TO SHOP IF THERE ARE NO 'IN STOCK' PRODUCTS IN THE PRODUCT TAG ARCHIVE PAGE */

function redirect_to_shop(){
    global $wp_query;

    if( is_woocommerce() && $wp_query->post_count == 0 ){
        the_post();
    $post_url = "/shop";
    wp_safe_redirect($post_url , 302 );
    exit;
    }
} 

add_action('template_redirect', 'redirect_to_shop');