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

时间:2021-02-11 16:31:50

标签: php wordpress woocommerce hook-woocommerce

我已经从 Woocommerce--> Inventory 中检查了从我的 eshop 中隐藏“缺货”产品的选项。现在,我试图找出如何从只有缺货产品的类别列表菜单小部件中隐藏类别。任何的想法?提前致谢。我使用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菜单插件上,负责在widget侧边栏显示类别的函数是

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' => '=',
    )
) 

但我有点困惑...

1 个答案:

答案 0 :(得分:0)

我们可以找到每个类别的帖子数的唯一方法是获取 Option Explicit Sub EmailCC() Dim xOTApp As Object Dim xMItem As Object Dim xCell As Range Dim xRg As Range Dim xEmailAddr As String Dim xTxt As String Dim w As Workbook Dim sProject As Worksheet Dim sContacts As Worksheet Dim i As Integer Dim column As Integer Set w = ActiveWorkbook Set sProject = w.Sheets(1) Set sContacts = w.Sheets(2) On Error Resume Next column = sProject.Cells(3, 9).Value If Not (IsNumeric(column)) Then Exit Sub Set xOTApp = CreateObject("Outlook.Application") i = 3 Do While Not sContacts.Cells(i, column).Value = "" xEmailAddr = sContacts.Cells(i, column).Value & ";" & xEmailAddr i = i + 1 Loop Set xMItem = xOTApp.CreateItem(0) With xMItem .To = " " .CC = xEmailAddr .Display End With End Sub 的所有类别 (terms)。然后我们循环遍历它们 - 每次调用我的函数 product_cat 并传递 get_count。在 term_id 中,我们运行查询以获取与其关联的所有帖子,并且我们还传递帖子元 get_count。这将返回库存状态为库存的所有产品。然后我们返回 _stock_status = instock 属性 - IF 语句然后检查帖子计数是否大于 0。

代码(已测试):

found_posts