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



我已经从Woocommerce->库存选项隐藏"缺货";我店里的产品。现在,我正试图找出如何从类别列表菜单小部件中隐藏一个类别,该类别只有缺货产品。任何想法?提前感谢。我使用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_categores_widget_args ?我使用iks菜单插件在手风琴上显示产品类别列表。上面的代码适用于Woocommerce产品类别小部件,但不适用于iks菜单小部件…

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

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)。然后我们循环遍历它们-每次调用我的函数get_count并传递term_id。在get_count中,我们运行一个查询来获取与它相关的所有帖子,我们也传递了_stock_status = instock的帖子元。这将返回库存状态为库存的所有产品。然后我们返回found_posts属性- IF语句然后检查post计数是否大于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;
}

相关内容

  • 没有找到相关文章

最新更新