web-dev-qa-db-ja.com

Woocommerceが製品を入手

次のコードを使用して、WordPress Webサイトの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ですべての製品を取得します。

私は製品が投稿として保存されていることを知っており、これを成し遂げようとしているが、そうは思えない。

特定のカテゴリの製品のリストを取得するにはどうすればよいですか?

37
Tester
<?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を変更します。

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

これはあなたが望むすべての製品をつかむはずです、私は投稿タイプに間違った投稿タイプを持っているかもしれませんが、私はwoo-commerceが投稿タイプに何を使用しているかをよく覚えていません。製品の配列を返します

24
Hunter WebDev

WP_Query()またはget_posts()を使用しないでください。 WooCommerceドキュメントから:

wc_get_productsとWC_Product_Queryは、安全に使用でき、将来のWooCommerceバージョンでのデータベースの変更によって破損しない製品を取得する標準的な方法を提供します。カスタムWP_Queriesまたはデータベースクエリを作成すると、パフォーマンスを向上させるためにデータがカスタムテーブルに移動するため、WooCommerceの将来のバージョンでコードが破損する可能性があります。

必要な製品を次のように取得できます。

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

WooCommerceドキュメント

22

特定のカテゴリまたはその他の分類から投稿/製品を取得するには、常に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();
0
Hritik Pandey