WooCommerce产品按类别排序

时间:2017-07-31 08:35:26

标签: php wordpress woocommerce

我有WooCommerce属性存档页面,我需要显示按类别排序的产品。像这样:

A类

  • 产品X
  • 产品Y
  • 产品Z

B类

  • 产品A
  • 产品B
  • 产品C

我现在的自定义循环的结果是:

A类

  • 产品A
  • 产品B
  • 产品C
  • 产品X
  • 产品Y
  • 产品Z

B类

  • 产品A
  • 产品B
  • 产品C
  • 产品X
  • 产品Y
  • 产品Z

因此它基本上显示了每个类别中属性术语中的所有产品,即使该产品甚至不在此类别中。我想我忽视了什么,但我无法弄清楚是什么?

<?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 1 for yes, 0 for no
$pad_counts     = 0; // 1 for yes, 0 for no
$hierarchical   = 1; // 1 for yes, 0 for no
$title          = '';
$empty          = 1; // 1 for hide, 0 for show

$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    'show_count'    => $show_count,
    'pad_counts'    => $pad_counts,
    'hierarchical'  => $hierarchical,
    'title_li'      => $title,
    'hide_empty'    => $empty,
);

$all_categories = get_categories( $args );

foreach ($all_categories as $cat) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

            $loop = new WP_Query( $args );

            while ( have_posts() ) : the_post();

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

    ?>

2 个答案:

答案 0 :(得分:0)

为获取类别明智的产品添加以下代码

@Override
        protected void populateViewHolder(MovieViewHolder viewHolder, final Movie model, final int position) {
            if(tvNoMovies.getVisibility()== View.VISIBLE){
                tvNoMovies.setVisibility(View.GONE);
            }
            viewHolder.tvMovieName.setText(model.getMovieName());
            viewHolder.address.setText(model.getAddress());
            viewHolder.phone.setText(model.getPhone());
            viewHolder.lit.setText(model.getLit());
            viewHolder.lon.setText(model.getLon());
            Picasso.with(MainActivity.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
         viewHolder.tvMovieName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, DetailActivity.class);
             startActivity(intent);
        }
    });

        }

答案 1 :(得分:0)

get_categories()WP_Query()的参数不同。

明智地获取产品类别的论据。

$args_for_product = array(
            'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
            'orderby'       => $orderby,
            'order'         => $order,
);

使用tax_query传递产品查询中的类别。其他参数(show_count,pad_counts,hierarchical,title_li,hide_empty未在WP_Query中传递)不需要。把上面的代码放在类别循环

还将while循环have_post()更改为$loop->have_post(),将the_post()更改为$loop->the_post()

  

完整代码

<?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 1 for yes, 0 for no
$pad_counts     = 0; // 1 for yes, 0 for no
$hierarchical   = 1; // 1 for yes, 0 for no
$title          = '';
$empty          = 1; // 1 for hide, 0 for show

$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    'show_count'    => $show_count,
    'pad_counts'    => $pad_counts,
    'hierarchical'  => $hierarchical,
    'title_li'      => $title,
    'hide_empty'    => $empty,
);

$all_categories = get_categories( $args );

foreach ($all_categories as $cat) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

        $args_for_product = array('type'=> 'product',
            'post_status'   => 'publish',
            'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
            'orderby'       => $orderby,
            'order'         => $order,
        );
            $loop = new WP_Query( $args_for_product );

            while ( $loop->have_posts() ) : $loop->the_post();

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

    ?>