【问题标题】:Woocommerce plugin - nested products for subcategoriesWoocommerce 插件 - 子类别的嵌套产品
【发布时间】:2015-12-11 16:09:17
【问题描述】:

我有一个自定义页面来显示与帖子相关的一些子类别产品。 我正在使用 woocommerce 短代码 [product_categories number="#" parent="#"]

唯一的问题是这个短代码只显示父类别下的子类别列表。

理想情况下,我很想拥有这样的东西:

SUBCAT 1

----- 产品1

----- 产品 2

----- 产品 3

SUBCAT 2

----- 产品1

----- 产品 2

----- 产品 3

等等。

有谁知道如何做到这一点?

非常感谢!

PS:我是 php 和 woocommerce 的新手...

【问题讨论】:

    标签: php wordpress plugins woocommerce


    【解决方案1】:

    您可以覆盖 archive.php 以 最终的结果会是这样的。

    • SUBCAT 1

      ----- 产品1

      ----- 产品 2

      ----- SUBCAT 1 的子类 1

      -------- 产品1

      -------- 产品2

      -------- 产品3

      SUBCAT 2

      ----- 产品1

      ----- 产品 2

      ----- 产品 3

    <?php
        // Remove breadcrumb if needed
        $post_id = yiw_post_id();
        if( get_post_meta( $post_id, '_show_breadcrumbs_page', true ) == 'no' ) {
            remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
        }
    
        if( get_post_meta( $post_id, '_slogan_page', true ) ) {
            get_template_part('slogan');
        }
        /**
         * woocommerce_before_main_content hook
         *
         * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
         * @hooked woocommerce_breadcrumb - 20
         */
        do_action('woocommerce_before_main_content');
    ?>
    
        <h1 class="page-title"><?php if( get_post_meta( yiw_post_id(), '_show_title_page', true ) == 'yes' ) woocommerce_page_title(); ?></h1>
    
        <div class="clear"></div>
    
        <?php if ( is_tax() && get_query_var( 'paged' ) == 0 ) : ?>
            <?php echo '<div class="term-description">' . yiw_addp( term_description() ) . '</div>'; ?>
        <?php elseif ( ! is_search() && get_query_var( 'paged' ) == 0 && ! empty( $shop_page ) && is_object( $shop_page ) ) : ?>
            <?php echo '<div class="page-description">' . apply_filters( 'the_content', $shop_page->post_content ) . '</div>'; ?>
        <?php endif; ?>
    
        <?php if ( have_posts() ) : ?>
            <?php
            $args = array(
                'taxonomy' => 'product_cat',
            );
    
            $cats = get_categories($args);
    
            foreach ( $cats as $cat ):
                if ( !$cat->parent ):
                    echo '<div class="clear"></div><h1>' . $cat->name . '</h1>';
    
                    // display products which are directly associated with the product category TODO: display only direct products
                    $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $cat->slug, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $cat->slug, 'include_children' => false  ) ) );
    
                    $wp_query = new WP_Query( $args );
    
                    ?>
                    <?php
                    /**
                    * woocommerce_pagination hook
                    *
                    * @hooked woocommerce_pagination - 10
                    * @hooked woocommerce_result_count - 20
                    * @hooked woocommerce_catalog_ordering - 30
                    */
                    do_action( 'woocommerce_before_shop_loop' );
                    ?>
    
                    <?php woocommerce_product_loop_start(); ?>
    
                    <?php woocommerce_product_subcategories(); $woocommerce_loop['loop'] = 0; $woocommerce_loop['columns'] = 0; ?>
    
                    <?php while ( have_posts() ) : the_post(); ?>
    
                        <?php woocommerce_get_template_part( 'content', 'product' ); ?>
    
                    <?php endwhile; // end of the loop. ?>
    
                    <?php                   
                    // get children product categories
                    $subcats = get_categories( array( 'hide_empty' => 0, 'parent' => $cat->term_id, 'taxonomy' => 'product_cat' ) );
    
                    if ( count($subcats) > 0 ):
                        $original_query = $wp_query;
    
                        foreach ( $subcats as $subcat ):
                            set_query_var('category_header_level', 2);
                            set_query_var('category', $subcat);
                            woocommerce_get_template_part( 'archive-product-child-category', 'loop' ); 
                        endforeach;
                        $wp_query = null;
                        $wp_query = $original_query;
                        wp_reset_postdata(); 
                    endif;
                    else:
                    /*  $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $cat->slug );
                        $wp_query = new WP_Query( $args );
                        ?>
                        <?php
                        /**
                        * woocommerce_pagination hook
                        *
                        * @hooked woocommerce_pagination - 10
                        * @hooked woocommerce_result_count - 20
                        * @hooked woocommerce_catalog_ordering - 30
                        */
                    /*  do_action( 'woocommerce_before_shop_loop' );
                        ?>
    
                        <?php woocommerce_product_loop_start(); ?>
    
                        <?php woocommerce_product_subcategories(); $woocommerce_loop['loop'] = 0; $woocommerce_loop['columns'] = 0; ?>
    
                        <?php while ( have_posts() ) : the_post(); ?>
    
                            <?php woocommerce_get_template_part( 'content', 'product' ); ?>
    
                        <?php endwhile; // end of the loop. ?>
                        <?php */
    
                    endif;
            endforeach;
            $wp_query = null;
            $wp_query = $original_query;
            wp_reset_postdata();
            ?>  
            <?php woocommerce_product_loop_end(); ?>
    
            <?php do_action('woocommerce_after_shop_loop'); ?>
    
        <?php else : ?>
    
            <?php if ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>
                <div class="clear"></div>
                <p><?php _e( 'No products found which match your selection.', 'woocommerce' ); ?></p>
    
            <?php endif; ?>
    
        <?php endif; ?>
    
    
        <div class="clear"></div>
    
        <?php
            /**
             * woocommerce_pagination hook
             *
             * @hooked woocommerce_pagination - 10
             * @hooked woocommerce_catalog_ordering - 20
             */
            do_action( 'woocommerce_pagination' );
        ?>
    
    <?php
        /**
         * woocommerce_after_main_content hook
         *
         * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
         */
        do_action('woocommerce_after_main_content');
    ?>
    
    <?php
        /**
         * woocommerce_sidebar hook
         *
         * @hooked woocommerce_get_sidebar - 10
         */
        do_action('woocommerce_sidebar');
    ?>
    

    并添加一个模板部分(extended_product_arcive_page_template.php)来处理层次结构。我已经为客户完成了。尝试实现这一点,如果您无法再次联系我。

    【讨论】:

    • 哇!!非常感谢你的普拉富拉。我想我想试试这个,但我不知道如何......我应该复制所有上述代码并将其粘贴到新模板部分(extended_product_archive_page_template.php)吗?那我该怎么办?如果您有一个电子邮件地址,以便我可以分享我正在开发的网站,那将是理想的。再次,谢谢你!!艾略特
    猜你喜欢
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2017-06-04
    • 2018-05-17
    • 1970-01-01
    相关资源
    最近更新 更多