Woocommerce按特定类别获取产品

时间:2014-10-15 08:13:21

标签: php wordpress woocommerce woothemes

我正在为woocommerce开发主题,我需要帮助来按类别从产品中检索信息,例如, 我希望显示来自'衬衫'类别限制的产品3个项目,这里是来自特色产品展示产品的woo主题的代码,(我试图改变按类别显示而不工作)

<ul class="featured-products products">

                <?php
                $args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
                $i = 0;                 
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); $_product; $i++; 

                if ( function_exists( 'get_product' ) ) {
                    $_product = get_product( $loop->post->ID );
                } else { 
                    $_product = new WC_Product( $loop->post->ID );
                }

                ?>

                        <li class="product <?php if ($i%3==0) echo ' last'; if (($i-1)%3==0) echo ' first'; ?>">
                            <div class="inner">
                            <?php woocommerce_show_product_sale_flash( $post, $_product ); ?>
                            <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                                <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
                                <h3><?php the_title(); ?></h3>
                                <span class="price"><?php echo $_product->get_price_html(); ?></span>
                            </a>
                            <?php woocommerce_template_loop_add_to_cart( $loop->post, $_product ); ?>
                            <?php smpl_product_more_details(); ?>
                            </div>
                        </li>

                <?php endwhile; ?>

                </ul>

我是新手,

提前致谢

4 个答案:

答案 0 :(得分:0)

使用以下代码替换$ args

$args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'terms'         => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
                'field'         => 'slug',
                'operator'      => $atts['operator']
            )
        )
    );

您只需将 ENTER_CATEGORY 字词替换为您要显示的类别名称。

如果这符合您的要求,请告诉我。

答案 1 :(得分:0)

$prod_categories = array(10, 27);
$product_args = array(
    'numberposts' => $limit,
    'post_status' => array('publish', 'pending', 'private', 'draft'),
    'post_type' => array('product', 'product_variation'),
    'orderby' => 'ID',
    'suppress_filters' => false,
    'order' => 'ASC',
    'offset' => 0
);

if (!empty($prod_categories)) {
    $product_args['tax_query'] = array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $prod_categories,
            'operator' => 'IN',
    ));
}

$products = get_posts($product_args);

答案 2 :(得分:0)

要按特定类别获取特色产品,您只需要使用wc_get_products,并将featured设置为true并指定类别。参见下面的代码。

<?php

// Get featured products by category. on this case its "shirts" which is the slug of the category.
$query_args = array(
    'featured' => true,  
    'category' => array( 'shirts' ),
);
$products = wc_get_products( $query_args );

您可以在此处https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/

查看完整的教程

答案 3 :(得分:0)

请使用以下查询

<ul class="products">
<?php
    $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

        <h2>Shoes</h2>

            <li class="product">    

                <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

                    <?php woocommerce_show_product_sale_flash( $post, $product ); ?>

                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>

                    <h3><?php the_title(); ?></h3>

                    <span class="price"><?php echo $product->get_price_html(); ?></span>                    

                </a>

                <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>

            </li>

<?php endwhile; ?>
<?php wp_reset_query(); ?>