【问题标题】:Woocommerce get products by specific categoriesWoocommerce 按特定类别获取产品
【发布时间】:2014-10-15 08:13:21
【问题描述】:

我正在为 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>

我是新手,

提前致谢

【问题讨论】:

    标签: php wordpress woocommerce woothemes


    【解决方案1】:

    要按特定类别获取特色产品,您只需使用 wc_get_products 并将特色设置为 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/

    【讨论】:

    【解决方案2】:
    $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);
    

    【讨论】:

      【解决方案3】:

      将 $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 单词替换为您要显示的类别名称即可。

      如果这满足您的要求,请告诉我。

      【讨论】:

        【解决方案4】:

        请使用下面的查询

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

        【讨论】:

          猜你喜欢
          • 2021-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-09
          • 2014-06-27
          • 1970-01-01
          • 2018-02-07
          • 2019-01-26
          相关资源
          最近更新 更多