在Woocommerce商店页面中仅显示特色产品

时间:2018-05-15 08:33:15

标签: php wordpress woocommerce product featured

我想在WooCommerce的默认商店页面中显示仅限特色产品,仅此而已...... 它有一个解决方案,只在WooCommerce商店模板中显示特色产品吗?

3 个答案:

答案 0 :(得分:0)

首先,您必须将archive-product.php模板覆盖到主题文件

然后添加以下代码以在商店pahe中显示特色产品。

    <?php 
                          $meta_query  = WC()->query->get_meta_query();
                          $tax_query   = WC()->query->get_tax_query();
                          $tax_query[] = array(
                              'taxonomy' => 'product_visibility',
                              'field'    => 'name',
                              'terms'    => 'featured',
                              'operator' => 'IN',
                          );

                          $args = array(
                              'post_type'           => 'product',
                              'post_status'         => 'publish',                          
                              'posts_per_page'      => '5',
                              'orderby'             => 'DESC',                          
                              'meta_query'          => $meta_query,
                              'tax_query'           => $tax_query,
                          );
                            $loop = new WP_Query( $args );

                            if ( $loop->have_posts() ) {
                                while ( $loop->have_posts() ) : $loop->the_post();
                                  $id = $product->get_id();
                                  $image_sale = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );
                                  $product_url = get_permalink($id);
                                  $product = wc_get_product($id);
                                  $product_title = $product->get_title();                              
                                  $sale_price =  $product->get_price();

                                  ?>

                     <div class="item">
                              <div class="product-box">
                                 <div class="product-img">
                              <a href="<?php echo $product_url;?>" title="" ><img src="<?php  echo $image_sale[0]; ?>" data-id="<?php echo $id; ?>"></a>                                    
                                 </div>
                                 <div class="product-content">
                                    <h5><?php echo $product_title;?></h5> 
                                    <P>$<?php echo $sale_price;?>,00</P>
                                 </div>
                              </div>
                         </div>


<?php 
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>

答案 1 :(得分:0)

你应该使用这个自定义函数挂钩woocommerce_product_query_tax_query过滤器钩子,它只显示商店中的特色产品(但不会显示在其他档案页面中):

// Display featured products in shop pages
add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
function custom_product_query_tax_query( $tax_query, $query ) {
    if( is_admin() ) return $tax_query;

    if ( is_shop() ) {
        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured'
        );
    }

    return $tax_query;
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试并正常工作。

答案 2 :(得分:0)

使用functions.php中的以下代码在商店页面中显示特色产品。

<?php 
add_action( 'woocommerce_product_query', 'ss_custom_product_query' );

function ss_custom_product_query( $q ){
    $meta_query = $q->get( 'meta_query' );
    if ( is_shop() ) {
        $meta_query[] = array(
            'key'   => '_featured',
            'value' => 'yes'
        );
    }
    $q->set( 'meta_query', $meta_query );
}
?>
相关问题