获得分组产品最高价格的woocommerce

时间:2017-05-02 08:01:11

标签: wordpress woocommerce product

我正在努力获得分组产品的最高价格,以便我只能在自定义模板页面上显示最高价格。我正在使用WP_Query获取产品列表。

以下是摘录:

                   $series_term = $_GET['series'];
                    $args=array(
                        'series'    => $term_name->slug,
                        'post_type' => 'product',
                        'posts_per_page' => 8,
                        'filter'    =>$valuecol,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'product_cat',
                                'field' => 'slug',
                                'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
                                'operator' => 'NOT IN'
                            )
                        )
                      );

                // $do_not_duplicate[] = $post->ID;
                $my_query = null;
                $my_query = new WP_Query($args);
                 /* Start the Loop */
                while ($my_query->have_posts()) : $my_query->the_post(); 
                $feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');                                 
                $feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
                             ?>
                    <div class="col-md-3 col-sm-4 col-xs-6">
                        <div class="product-detail">
                            <div class="product-thmbnial">
                                <a href="<?php the_permalink(); ?>">
                                    <img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
                                </a>
                            </div>
                            <div class="info"><p><a href="<?php the_permalink();?>"><?php  the_title(); ?></a><br/>
                                <span>
                                <?php $wp_product = new WC_Product( get_the_ID() );  
                                echo get_woocommerce_currency_symbol(); ?></sup><?php 
                            echo $wp_product->get_price();?></span></p></div>
                        </div>
                    </div>
                <?php endwhile; ?>

我希望分组产品最高价格代替$ wp_product-&gt; get_price();

我试图让儿童产品,但阵列是空的。

    foreach ( $wp_product->get_children() as $child_id ) {
    $child_prices[] = get_post_meta( $child_id, '_price', true );
}

任何人都可以建议我如何获得最高价格。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案..我之前尝试过相同的解决方案,但那段时间它没有工作。

以下是修复:

 $series_term = $_GET['series'];
                    $args=array(
                        'series'    => $term_name->slug,
                        'post_type' => 'product',
                        'posts_per_page' => 8,
                        'filter'    =>$valuecol,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'product_cat',
                                'field' => 'slug',
                                'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
                                'operator' => 'NOT IN'
                            )
                        )
                      );

                // $do_not_duplicate[] = $post->ID;
                $my_query = null;
                $my_query = new WP_Query($args);
                 /* Start the Loop */
                while ($my_query->have_posts()) : $my_query->the_post(); 
                $feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');                                 
                $feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
                             ?>
                    <div class="col-md-3 col-sm-4 col-xs-6">
                        <div class="product-detail">
                            <div class="product-thmbnial">
                                <a href="<?php the_permalink(); ?>">
                                    <img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
                                </a>
                            </div>
                            <div class="info"><p><a href="<?php the_permalink();?>"><?php  the_title(); ?></a><br/>
                                <span>
                                <?php $wp_product = new WC_Product( get_the_ID() );  
                                $child_prices = array();
                                foreach ( $product->get_children() as $child_id ) {
                                    $child_prices[] = get_post_meta( $child_id, '_price', true );
                                }
                                $child_prices = array_unique( $child_prices );
                                if ( ! empty( $child_prices ) ) {
                                    $min_price = min( $child_prices );
                                    $max_price = max( $child_prices );
                                } else {
                                    $min_price = '';
                                    $max_price = '';
                                }
                                if($min_price == $max_price && !empty($min_price))
                                {
                                    echo get_woocommerce_currency_symbol(); ?></sup><?php
                                    echo $min_price;
                                }
                                elseif(!empty($max_price))
                                {
                                    echo get_woocommerce_currency_symbol(); ?></sup><?php
                                    echo $max_price;
                                }
                                else
                                {
                                    echo get_woocommerce_currency_symbol(); ?></sup><?php
                                    echo $wp_product->get_price();
                                }
                            ?></span></p></div>
                        </div>
                    </div>
                <?php endwhile; ?>
相关问题