[英]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.