WooCommerce - 如何在标签云中没有“库存"产品时隐藏产品标签

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

下面的代码显示了我目前的进度,我会随着更多的进展进行更新.我相信此代码可用于运行检查并删除任何显示计数"值小于 1 的结果,这将回答这个问题.此外,隐藏的结果需要 302 重定向到顶级商店"页面,同时暂时隐藏:

/* 将产品标签云转换为标签总数可见的字母列表 */功能 woocommerce_product_tag_cloud_widget_filter($args) {$args = 数组('最小' =>14,'最大' =>14,'格式' =>'列表','分类' =>'产品标签','单位' =>'像素','show_count' =>1、'数字' =>0,);echo "

我也有这段代码,可以用作第二种方法.注释显示了缺少的内容:

/* 当库存结果少于 1 个时,从产品标签云中删除产品标签 */功能 filter_woocommerce_product_tag_cloud_widget_args( $array ) {$c =//检查产品标签存档有多少库存产品如果($c <= 1){//显示不包括空产品标签档案的数组}别的{返回$数组;}}add_filter('woocommerce_product_tag_cloud_widget_args', 'filter_woocommerce_product_tag_cloud_widget_args', 10, 1);

我有以下资源可以提供帮助,但是我缺乏 PHP 知识意味​​着我无法将这些不同的代码段放入一个函数中以实现我想要的结果.

  • 如何在 WooCommerce 中仅查询状态为库存"的产品?

  • 在 Woocommerce 中隐藏缺货"产品

我有这段代码来自 如何从 woocommerce 上的标签云中删除空产品标签 我之前的问题的答案代码,该问题从标签云中隐藏了所有空产品标签(但它没有t 考虑库存可用性,因此仍会显示带有 0 个库存"产品的产品标签).

下面还有其他一些代码,它们可能对最终解决方案有所帮助,也可能无济于事.

此代码隐藏了整个产品标签云.

add_action('widgets_init', 'misha_remove_product_tag_cloud_widget');功能 misha_remove_product_tag_cloud_widget(){unregister_widget('WC_Widget_Product_Tag_Cloud');}

此代码隐藏所有有 0 个库存"产品的类别.

add_filter('wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3);功能 nav_remove_empty_category_menu_item ($items, $menu, $args) {全球 $wpdb;$nopost = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0");foreach ( $item as $key => $item ) {if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {未设置($items[$key]);}}返回$项目;}

我试图将上述代码片段的各种元素组合在一起以创建一个实现我的目标的函数,但我缺乏能力意味着我没有任何东西可以正确工作,因此我需要额外的帮助.

解决方案

我已经设法将这个问题的以下解决方案拼凑在一起,实际上它更进一步,并添加了一些我试图解释的其他很酷的功能注释代码.

如果有人对此代码有任何反馈、补充、意见或问题,请务必与我们联系!我将尝试扩展此代码,以便通过仅显示与网站内用户位置相关的标签并从这些级别删除所有不相关的标签,它还可以兼作优化功能 - 欢迎任何建议!

特别感谢 Facebook 上的 Md. Mehedi Hasan 帮助编写中间部分代码以从前端隐藏产品标签存档页面.

/* 将产品标签云转换为标签总数可见的字母列表 */功能 woocommerce_product_tag_cloud_widget_filter($args) {$args = 数组('最小' =>14,'最大' =>14,'格式' =>'列表','分类' =>'产品标签','单位' =>'像素','show_count' =>1、'数字' =>0,);echo "<div style='padding-left: 20px;最小高度:50px;最大高度:400px;溢出:自动;'>";返回 $args;echo "</div>";}add_filter('woocommerce_product_tag_cloud_widget_args', 'woocommerce_product_tag_cloud_widget_filter');/* 当他们没有库存"产品时隐藏产品标签存档页面 */函数 hide_empty_tags( $terms, $taxonomies) {$new_terms = 数组();if ( in_array( 'product_tag', $taxonomies ) && !is_admin() ) {foreach ( $terms as $key => $term ) {if ($term->count >0){$new_terms[] = $term;}}$terms = $new_terms;}返回$条款;}add_filter('get_terms', 'hide_empty_tags', 10, 3);/* 如果产品标签存档页面中没有库存"产品,则重定向到商店 */功能重定向到商店(){全局 $wp_query;if(is_woocommerce() && $wp_query->post_count == 0){the_post();$post_url = "/shop";wp_safe_redirect($post_url, 302);出口;}}add_action('template_redirect', 'redirect_to_shop');

I have a WooCommerce site and need product tags to be hidden from the product tag cloud when a product tag archive page has 0 'in stock' products available.

The code below shows my current progress and I will update as more progress is made. I believe this code could be adapted to run a check and remove any results which have a 'show count' value less than 1 which would answer this question. Additionally the hidden results will need to 302 redirect to the top level 'shop' page whilst it is temporarily hidden:

/* 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');

I also have this code which might be used as a second approach. The notes show what is missing:

/* REMOVE PRODUCT TAGS FROM THE PRODUCT TAG CLOUD WHEN THEY HAVE LESS THAN 1 IN-STOCK RESULTS */

function filter_woocommerce_product_tag_cloud_widget_args( $array ) { 
    $c = // check to see how many in stock products a product tag archive has
        if($c <= 1){
            // show array excluding empty product tag archives
        }else{
            return $array;
        } 
}

add_filter( 'woocommerce_product_tag_cloud_widget_args', 'filter_woocommerce_product_tag_cloud_widget_args', 10, 1 );

I have the following resources to help, however my lack of PHP knowledge means I am unable to put these various pieces of code into a single function to achieve my desired outcome.

  • How to query only for products with status "in stock" in WooCommerce?

  • Hide 'out of stock' products in Woocommerce

I have this piece of code from How to remove empty product tags from the tag cloud on woocommerce answer code to my previous question which hides all empty product tags from the tag cloud (but it doesn't factor in stock availability and so will still show product tags with 0 'in stock' products).

I have also some others pieces of code below which may or may not help with a final solution.

This code hides the entire product tag cloud.

add_action( 'widgets_init', 'misha_remove_product_tag_cloud_widget' );
 
function misha_remove_product_tag_cloud_widget(){
    unregister_widget('WC_Widget_Product_Tag_Cloud');
}

This code hides all categories which have 0 'in stock' products.

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;
}

I have tried to put various elements of the above pieces of code together to create a function that achieves my goal, but my lack of ability means I have not got anything to correctly work and therefore I need additional help.

解决方案

I have managed to piece together the following solution to this question that actually takes it a few steps further and adds some other cool functionality which I have tried to explain in the comment code.

If anyone has any feedback, additions, comments or questions about this code, please do get in touch! I will be trying to extend on this code so that it can also double up as a refine feature by only showing tags relevant to a users position within the website and removing all irrelevant tags from those levels - Any suggestions are welcomed!

Special thanks to Md. Mehedi Hasan on Facebook for helping with the middle section of code to hide the product tag archive pages from the front end.

/* 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');

相关文章