Woocommerce获得产品

时间:2014-01-09 18:58:25

标签: php wordpress woocommerce

我使用以下代码在我的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的所有产品。

我知道产品存储为帖子,并且一直试图完成这项工作,但似乎无法实现。

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

4 个答案:

答案 0 :(得分:61)

<?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。

答案 1 :(得分:24)

<?php
$args     = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args ); 
?>

这应该抓住你想要的所有产品,我可能有错误的帖子类型虽然我不太记得woo-commerce用于帖子类型。它将返回一系列产品

答案 2 :(得分:18)

请勿使用WP_Query()get_posts()。来自WooCommerce doc:

  

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

您可以检索您想要的产品:

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

WooCommerce documentation

答案 3 :(得分:0)

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

$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();
相关问题