WooCommerce - 当没有“库存"产品时,如何在标签云中隐藏产品标签 [英] WooCommerce - How to hide product tags in the tag cloud when it has no 'in stock' products

查看:18
本文介绍了WooCommerce - 当没有“库存"产品时,如何在标签云中隐藏产品标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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.

在 Woocommerce 中隐藏缺货"产品

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

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!

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

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

这篇关于WooCommerce - 当没有“库存"产品时,如何在标签云中隐藏产品标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆