【问题标题】:How to remove empty product tags from the tag cloud on woocommerce [duplicate]如何从 woocommerce 上的标签云中删除空产品标签 [重复]
【发布时间】:2020-10-02 20:45:06
【问题描述】:

我看过很多类似的帖子,但没有一个专门解决我遇到的问题。

我需要创建一个函数,允许我从我的 woocommerce 网站上的标签云中删除空标签存档页面,这样它们就不会引导用户进入空页面。

我发现的唯一允许删除整个标签云的代码是:

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

我相信这可以与这样的代码结合使用,它允许从各个位置删除所有未使用的类别:

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

我不确定如何将两者结合使用,但最终结果应该允许将任何未使用的产品标签存档页面从标签云中隐藏起来,直到它们再次被使用或完全从网站上删除 - 但始终阻止用户从访问空标签页!

【问题讨论】:

    标签: php wordpress woocommerce widget taxonomy-terms


    【解决方案1】:

    通常情况下,WooCommerce 产品标签云小部件会隐藏空标签……如果不是这样,您可以使用:

    add_filter('woocommerce_product_tag_cloud_widget_args','hide_empty_terms_from_product_tag_cloud_widget');
    function hide_empty_terms_from_product_tag_cloud_widget( $args ) {
        $args['hide_empty'] = true; // Empty tags not visible
        return $args;
    }
    

    要在 WooCommerce 产品标签云小部件中显示空产品标签,请使用:

    add_filter('woocommerce_product_tag_cloud_widget_args','show_empty_terms_from_product_tag_cloud_widget');
    function show_empty_terms_from_product_tag_cloud_widget( $args ) {
        $args['hide_empty'] = false; // Empty tags are visible
        return $args;
    }
    

    wp_tag_cloud()(used by WooCommerce tag Cloud Widget)get_terms()函数。

    【讨论】:

    • 不幸的是,我的问题措辞可能很糟糕。虽然我知道这将从标签云中隐藏空标签,因为标签不会变空,而是具有零库存的产品(因此隐藏在网站上)标签云仍然显示这些产品。我需要标签变得不可见,不仅当它们完全为空时,而且当它们只包含缺货产品时......有没有办法将此检查放入您的代码中?
    【解决方案2】:

    这个问题在这里得到了更深入的回答:WooCommerce - How to hide product tags in the tag cloud when it has no 'in stock' products

    作为最终解决方案的代码如下:

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

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 2021-07-18
      • 2019-10-22
      相关资源
      最近更新 更多