繁体   English   中英

当该类别下的所有产品都缺货时,如何在 woocommerce 类别列表菜单上隐藏产品类别?

[英]How can i hide a product category on woocommerce category list menu when all the products under this category are out of stock?

我已经从 Woocommerce-> Inventory 中检查了从我的电子商店中隐藏“缺货”产品的选项。 现在,我试图找出如何从只有缺货产品的类别列表菜单小部件中隐藏一个类别。 任何想法? 提前致谢。 我使用 iks 菜单插件。

我试过了

function exclude_categories( $category_list_args ) {

$args = array(
    'hide_empty' => false,
    'hierarchical' => true,
);

$product_categories = get_terms( 'product_cat', $args );

$exclude = array();
foreach ( $product_categories as $category ) {

    $posts         = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->slug, 'fields' => 'ids' ) );
    $show_category = false;

    foreach ( $posts as $post ) {

        $product         = new wC_Product( $post );
        $visible_product = $product->is_visible();

        if ( true === $visible_product ) {
            $show_category = true;
            break;
        }

    }

    if ( false === $show_category ) {
        $exclude[] = $category->term_id;
    }

}

if ( ! empty( $exclude ) ) {
    $category_list_args['exclude'] = implode( ',', $exclude );
    unset( $category_list_args['include'] );
}

return $category_list_args;

}
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_categories', 10, 1 );

我应该使用哪个钩子而不是 woocommerce_product_categories_widget_args? 我使用 iks menus 插件在手风琴上显示产品类别列表。 上面的代码适用于 Woocommerce 产品类别小部件,但不适用于 iks 菜单小部件......

在 iks 菜单插件上,负责在小部件侧边栏上显示类别的 function 是

private function get_WP_terms() {
    $taxonomy = $this->get_taxonomy();

    if ( $taxonomy ) {
        $this->args = [
            "taxonomy" => $taxonomy,
        ];
        $this->save_args( [
            "orderby",
            "order",
            "hide_empty",
            "hierarchical",
            "include",
            "exclude",
            "search",
            "child_of",
            "parent",
            "childless",
        ] );

        if ( version_compare( get_bloginfo( 'version' ), '4.5', '>=' ) ) {
            $items = get_terms( $this->args );
        } else {
            $items = get_terms( $taxonomy, $this->args );
        }

        if ( is_array( $items ) ) {
            if ( ! empty( $items ) ) {
                $index      = 0;
                $show_posts = $this->settings_manager->get_value( "show_posts" );

                /* Posts */
                if ( $show_posts ) {
                    $post_type   = Utils::get_post_type_by_taxonomy( $taxonomy );
                    $include     = $this->settings_manager->get_value( "include" );
                    $has_include = ! empty( $include );

                    $posts = get_posts( [
                        'post_type'        => $post_type,
                        'posts_per_page'   => - 1,
                        'orderby'          => 'date',
                        'order'            => 'DESC',
                        'tax_query'        => [
                            [
                                'taxonomy' => $taxonomy,
                                'operator' => $has_include ? 'IN' : 'EXISTS',
                                'terms'    => $has_include ? Utils::get_terms_ids( $items ) : [],
                            ]
                        ],
                         
                        'suppress_filters' => false // [Fix] WPML compatibility
                    ] );

                    $posts_by_terms = [];
                    foreach ( $posts as $post ) {
                        $terms = get_the_terms( $post, $taxonomy );
                        if ( ! empty( $terms ) ) {
                            foreach ( $terms as $term ) {
                                if ( ! isset( $posts_by_terms[ $term->term_id ] ) ) {
                                    $posts_by_terms[ $term->term_id ] = [];
                                }
                                $posts_by_terms[ $term->term_id ][] = $post;
                            }
                        }
                    }
                }

                foreach ( $items as $key => $item ) {
                    $id   = (int) $item->term_id;
                    $link = get_term_link( $id );

                    $item_data = [
                        "id"                    => $id,
                        "title"                 => $item->name,
                        "link"                  => $link,
                        "parent"                => (int) $item->parent,
                        "is_current"            => $this->is_current_page_url( $link ) || $this->get_queried_object_term_id() === $id,
                        "is_term_includes_post" => $this->is_term_includes_post( $id, $this->get_taxonomy() ),
                        "is_page_includes_post" => false,
                        "index"                 => $index,
                        "is_expanded"           => false,
                        "posts_count"           => $item->count,
                        "is_post"               => false,
                        "target"                => null,
                    ];
                    $index ++;

                    $this->data[] = $item_data;

                    if ( $show_posts ) {
                        if ( isset( $posts_by_terms[ $id ] ) ) {
                            $result_posts = $posts_by_terms[ $id ];
                            foreach ( $result_posts as $post ) {
                                $post_link    = get_permalink( $post->ID );
                                $this->data[] = [
                                    "id"                    => $post->ID,
                                    "title"                 => $post->post_title,
                                    "link"                  => $post_link,
                                    "parent"                => $id,
                                    "is_current"            => $this->is_current_page_url( $post_link ),
                                    "is_term_includes_post" => false,
                                    "is_page_includes_post" => false,
                                    "index"                 => $index,
                                    "is_expanded"           => false,
                                    "posts_count"           => false,
                                    "is_post"               => true,
                                    "target"                => null,
                                ];
                                $index ++;
                            }
                        }
                    }
                }
            }

我想我需要类似的东西

  'meta_query' => array(
    array(
        'key' => '_stock_status',
        'value' => 'instock',
        'compare' => '=',
    )
) 

但我有点困惑......

我们可以找到每个类别的帖子数的唯一方法是获取product_cat的所有类别( terms )。 然后我们遍历它们——每次调用我的 function get_count并传递term_id get_count ,我们运行查询以获取与其关联的所有帖子,并且我们还传递了 post meta _stock_status = instock 这将返回库存状态为库存的所有产品。 然后我们返回found_posts属性 - IF 语句然后检查帖子计数是否大于 0。

代码(已测试):

add_shortcode('test_test','test_test');
function test_test() {

    $terms = get_terms('product_cat');

    foreach($terms as $term) {

        $post_count = get_count($term->term_id);

        if($post_count > 0) {
            echo $term->name . '<br>'; //This is where you put your category details
        }  
    }
}

function get_count($cat_id) {

    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => array($cat_id),
                'include_children' => false
              )
        ),
        'posts_per_page' => -1,
        'post_type' => 'product',
        'post_status' => 'publish',
        'meta_key' => '_stock_status',
        'meta_value' => 'instock',
        'meta_compare' => '='
    );

    $wp_query = new WP_Query($args);

    wp_reset_postdata();

    return $wp_query->found_posts;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM