【问题标题】:Woocommerce get productsWoocommerce 获取产品
【发布时间】:2014-01-28 12:17:21
【问题描述】:

我使用以下代码在我的 WordPress 网站中从 WooCommerce 获取产品类别列表:

 <?php
  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 0;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;
$args = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
  'hide_empty'   => $empty
);
?>
<?php $all_categories = get_categories( $args );

//print_r($all_categories);
foreach ($all_categories as $cat) {
    //print_r($cat);
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;

?>     

<?php       

        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; ?>


        <?php
        $args2 = array(
          'taxonomy'     => $taxonomy,
          'child_of'     => 0,
          'parent'       => $category_id,
          'orderby'      => $orderby,
          'show_count'   => $show_count,
          'pad_counts'   => $pad_counts,
          'hierarchical' => $hierarchical,
          'title_li'     => $title,
          'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            }

        } ?>



    <?php }     
}
?>

这可以正常工作并返回产品类别列表。我现在一直在尝试获取特定类别的产品列表。

示例:使用cat_id=34 获取所有产品。

我知道产品以帖子的形式存储,并且一直在尝试完成此操作,但似乎无法完成。

如何获取特定类别的产品列表?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:
    <?php  
        $args = array(
            'post_type'      => 'product',
            'posts_per_page' => 10,
            'product_cat'    => 'hoodies'
        );
    
        $loop = new WP_Query( $args );
    
        while ( $loop->have_posts() ) : $loop->the_post();
            global $product;
            echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
        endwhile;
    
        wp_reset_query();
    ?>
    

    这将列出所有产品缩略图和名称以及它们到产品页面的链接。根据您的要求更改类别名称和posts_per_page。

    【讨论】:

    • 如果您想在代码的嵌套内循环中使用它,请使用 'product_cat' => $sub_category->slug。
    • 你们中的一些人简直就是天才。应该有一种奖励非常有帮助的评论/人的方式:)
    • 我想使用get_posts( arg)。我该怎么做?
    • 通过这种方式获取帖子对象,如果我想获取产品对象我应该使用什么?
    • 谢谢。正是我想要的。
    【解决方案2】:

    不要使用WP_Query()get_posts()。来自 WooCommerce 文档:

    wc_get_products 和 WC_Product_Query 提供了一种检索产品的标准方法,该方法可以安全使用并且不会因未来 WooCommerce 版本中的数据库更改而中断。随着数据向自定义表移动以获得更好的性能,构建自定义 WP_Queries 或数据库查询可能会破坏您在未来版本的 WooCommerce 中的代码。

    你可以像这样检索你想要的产品:

    $args = array(
        'category' => array( 'hoodies' ),
        'orderby'  => 'name',
    );
    $products = wc_get_products( $args );
    

    WooCommerce documentation

    注意:category 参数采用 slug 数组,而不是 ID。

    【讨论】:

    • 这不适用于类别 ID。它需要类别 slug。
    • @Himadri 你似乎是对的。文档只提到了蛞蝓。我将修改示例。
    【解决方案3】:
    <?php
    $args     = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
    $products = get_posts( $args ); 
    ?>
    

    这应该会获取您想要的所有产品,我可能有错误的帖子类型虽然我不太记得 woo-commerce 用于帖子类型的内容。它将返回一个产品数组

    【讨论】:

    • 我试过你的 sn-p 但它只返回 5 个产品虽然我试过 post_per_page = -1。你能告诉我为什么吗?
    • @huykon225 正确的参数是posts_per_page,而不是post_per_page
    • @huykon225 没问题!
    【解决方案4】:

    始终使用 tax_query 从特定类别或任何其他分类中获取帖子/产品。您还可以使用特定分类的 ID/slug 获取产品...

    $the_query = new WP_Query( array(
        'post_type' => 'product',
        'tax_query' => array(
            array (
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'accessories',
            )
        ),
    ) );
    
    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        the_title(); echo "<br>";
    endwhile;
    
    
    wp_reset_postdata();
    

    【讨论】:

    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多