获取每个WooCommerce父类别的现有子类别的总数

时间:2017-08-17 08:07:49

标签: php wordpress woocommerce categories product

我已经编辑了woocommerce archive-product.php文件来实现我的布局,但还有一件事我需要弄清楚。我想在每页的档案中显示现有子类别的总数。

例如,我有两个主要产品类别(A,B),每个产品类别都有一些子类别:

SELECT * FROM (SELECT ROUND(CAST(val * CAST(0.01 AS NUMERIC(2,2)) / CAST(12 AS INT) AS DECIMAL(15, 9)), 9) AS val FROM [value] AS pr) a

我想显示每个父母的子类别的数量,在我的案例中,A档案页面中的2个子类别和B档案页面中的3个子类别。

这是我到目前为止所做的:

Category A
    Subcategory A1
    Subcategory A2

Category B
    Subcategory B1
    Subcategory B2
    Subcategory B3

这是与我的问题相关的部分:

    <?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.0.0
 */

 include '/../templates/aq_resizer.php';


if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

get_header( 'shop' ); ?>

        <?php
             $term = $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  
    $classes = get_body_class();
?>
    <?php if( get_field('top_image') ): ?>
                   <div  class="top-bg" style="background-image:url('<?php the_field('top_image', $taxonomy . '_' . $term_id); ?>'); ">
                           <div class="container">
                        <div class="filter-cat">
                                  <div id="search-container"  class="container">
                                           <?php echo do_shortcode( '[searchandfilter id="85"]' ); ?>
                                   </div>                   
                              </div>
                     </div>
                   </div>
           <?php else: ?>
                <div  class="top-bg" style="background-image:url('<?php bloginfo('template_url') ?>/images/Product-Page.jpg'); ">
                     <div class="container">
                        <div class="filter-cat">
                                  <div id="search-container"  class="container">
                                           <?php echo do_shortcode( '[searchandfilter id="85"]' ); ?>
                                          <?php 

function woocommerceCategorySlug( $id ){
    $term = get_term_by('id', $id, 'product_cat', 'ARRAY_A');
    return $term['slug'];       
}

?>
                                   </div>                   
                              </div>
                     </div>
                </div>
          <?php endif; ?>


            <h2 class="container taxonomy-title">

                   <?php if (in_array('tax-product_cat',$classes)): ?>

                                   <?php echo  $term->name ?>
            <?php else: ?>

                         <?php

                $url = $_SERVER["REQUEST_URI"];
                        $url = explode("=", $url);
                        $url = urldecode($url[count($url) - 1]);
                             $str = str_replace("-", " ", "$url");

                        if(strpos($_SERVER['REQUEST_URI'], '_cat') !== false){

                        echo "$str";
                    } else {
                        echo 'Products';
                    }

                ?>

        <?php endif; ?>

    </h2>

    <?php
        /**
         * woocommerce_before_main_content hook.
         *
         * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
         * @hooked woocommerce_breadcrumb - 20
         * @hooked WC_Structured_Data::generate_website_data() - 30
         */
        do_action( 'woocommerce_before_main_content' );
    ?>

    <header class="woocommerce-products-header">

        <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>

            <!--<h1 class="woocommerce-products-header__title page-title ptbl"><?php woocommerce_page_title(); ?></h1>-->

        <?php endif; ?>

        <?php
            /**
             * woocommerce_archive_description hook.
             *
             * @hooked woocommerce_taxonomy_archive_description - 10
             * @hooked woocommerce_product_archive_description - 10
             */
            //do_action( 'woocommerce_archive_description' );
        ?>

    </header>






        <?php if ( have_posts() ) : ?>

            <?php woocommerce_product_loop_start(); ?>

                <div class="subcategories container">
                    <?php if ( woocommerce_product_subcategories() ): ?>

                    <?php

                              $taxonomyName =get_query_var( 'product_cat' ); // use use get_queried_object()->taxonomy; to get the current taxonomy name
                              $countchildren = count (get_term_children( $term_id, $taxonomyName ));

                     echo '<h3 class="group-products mts mbm  cat-title text-underline">'.'<span class="big">'.'Products Families'.'<span class="cont-post">'.'(' .$countchildren.')'.'</span>'.'</span>'.'</h3>';

                    ?>
                      <?php endif; ?>

                </div>
                    <?php
                /**
                 * woocommerce_before_shop_loop hook.
                 *
                 * @hooked wc_print_notices - 10
                 * @hooked woocommerce_result_count - 20
                 * @hooked woocommerce_catalog_ordering - 30
                 */
                do_action( 'woocommerce_before_shop_loop' );
            ?>




                    <div class="list-products container">
                        <?php $c=1; 
                        ?>

                <?php while ( have_posts() ) : the_post(); ?>


                    <?php
                        /**
                         * woocommerce_shop_loop hook.
                         *
                         * @hooked WC_Structured_Data::generate_product_data() - 10
                         */
                        do_action( 'woocommerce_shop_loop' );
                    ?>
                    <div class="col-sm-3">
                        <a class="product-image" data-fancybox data-type="iframe" data-src="<?php echo the_permalink( $product->id ); ?>" href="javascript:;" id="product_<?php echo $c; ?>">
                              <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
                              $thumb_url = $image[0];

                               $image_src = aq_resize( $thumb_url, 260, 209, true );    
                               ?>
                              <img src="<?php echo $image_src; ?>" />
                              <h3 class="woocommerce-loop-category__title"><?php the_title(); ?></h3>
                          </a>


                    <?php $c++; ?>

                    </div>
                <?php endwhile; // end of the loop. ?>
                </div>
            <?php woocommerce_product_loop_end(); ?>

            <?php
                /**
                 * woocommerce_after_shop_loop hook.
                 *
                 * @hooked woocommerce_pagination - 10
                 */
                do_action( 'woocommerce_after_shop_loop' );
            ?>

        <?php elseif ( ! woocommerce_product_subcategories( array( 'before' => woocommerce_product_loop_start( false ), 'after' => woocommerce_product_loop_end( false ) ) ) ) : ?>

            <?php
                /**
                 * woocommerce_no_products_found hook.
                 *
                 * @hooked wc_no_products_found - 10
                 */
                do_action( 'woocommerce_no_products_found' );
            ?>

        <?php endif; ?>

    <?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 get_footer( 'shop' ); ?>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以使用get_term_children()函数以这种方式计算子类别:

$countchildren = count( get_term_children( $queried_object->term_id, 'product_cat' ));

或者

$countchildren = count( get_term_children( $term_id, 'product_cat' ));

或者

$countchildren = count( get_term_children( get_queried_object()->term_id, get_queried_object()->taxonomy ));

这3个工作正常。

  

但不要使用:

$taxonomyName =get_query_var( 'product_cat' );
$countchildren = count (get_term_children( $term_id, $taxonomyName ));
     

$taxonomyName 实际上是您的主要类别slug ,但不是分类标记:

$taxonomy = $queried_object->taxonomy;