代码之家  ›  专栏  ›  技术社区  ›  Reece

Woomcommerce产品循环数据

  •  0
  • Reece  · 技术社区  · 6 年前

    我希望能够获取shortcode[Best_Selling_Products limit=“4”]所做的所有内容,但必须在wp_查询循环中完成。这有可能吗?

    这就是我目前为止所拥有的;

    <?php   
        $args = array(
            'post_type' => 'product',
            'meta_key' => 'total_sales',
            'orderby' => 'meta_value_num',
            'posts_per_page' => 4,
        );
        $query = new WP_Query( $args );
    ?>
    
    <?php if($query->have_posts()): ?>
        <div class="container" id="best_sellers">
            <div class="row">
            <?php while( $query->have_posts() ): $query->the_post(); ?>
                <div class="col-md-3">
    
                </div>
            <?php endwhile; ?>
            </div>
        </div>
    <?php endif; ?> 
    

    我知道我可以抓取标题,价格,链接,缩略图等,但我不知道如何抓取是添加到购物车按钮。是否可以在循环中得到它,或者必须创建一个?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Metalik    6 年前

    首先在while循环中获取product对象:

    $product = wc_get_product(get_the_ID());
    

    然后,您可以通过以下方式获得添加到购物车URL和文本:

    $product->add_to_cart_url()
    $product->add_to_cart_text()
    
        2
  •  1
  •   Elvin Haci    6 年前

    是的,你可以。

    <?php while( $query->have_posts() ): $query->the_post(); ?>
        <div class="col-md-3">
         <?php 
         do_action( 'woocommerce_shop_loop' );
         wc_get_template_part( 'content', 'product' );
         ?>
         </div>
    <?php endwhile; ?>
    

    有关如何呈现WooCommerce循环内容的详细信息,可以查看wp content/plugins/WooCommerce/templates目录中的WooCommerce模板文件。