Woocommerce get products

I used the following code to get the list of product categories form WooCommerce in my WordPress website:

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

This works fine and returns the list of product categories. I have been trying now to get a list of products for a particular category.

Example: get all the products for with cat_id=34.

I know products are stored as posts, and have been trying to get this done but can't seem to.

How do I get the list of products for a particular category?

Solutions

Do not use WP_Query() or get_posts(). From the WooCommerce doc:

wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.

You can retrieve the products you want like this:

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

WooCommerce documentation

Note: the category argument takes an array of slugs, not IDs.

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

This will list all product thumbnails and names along with their links to product page. change the category name and posts_per_page as per your requirement.

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

This should grab all the products you want, I may have the post type wrong though I can't quite remember what woo-commerce uses for the post type. It will return an array of products

Similar questions

How to sort products by price ASC and place all no price products to the end? (Woocommerce)
I have such trouble, tried a lot of solutions, but nothing worked for me. I need to show all 'no price' products in the end of catalog, when I am sorting products by price ASC in Woocommerce.
WooCommerce: exclude recent products from all products
I want to show a number of recent products and than all other without those in recent. I know that it's possible to do with posts in Wordpress by using 'offset' but I have no idea what to do with Woocommerce. Any idea how to exclude first N products from all of them?
WooCommerce Variable Products - Can't choose products with '0' as an attribute - Error 'please choose all product options'
I've been developing an e-commerce store for a client using WooCommerce and I've come across a very strange issue. All of my products are variable products and I have set them up like this: Variation 1 - Weight (0,0.5,1.0,1.5,2.0...5.0 in Kgs) Variation 2 - Units (0,1,2,3,4,5...10) Variation 3 - Cut Type (Ground, Slices, Whole etc) I set the variat...
Fetch child products in grouped products woocommerce
I want to fetch child product ids of a parent grouped product, how to do that, earlier woocommerce saves the child array in wp_option table as key to be _transient_wc_product_children_ids_8 but the recent update has changed the way it saves to the database, plz help me, how can I fetch child products from parent product in grouped products.
Display Woocommerce Products as well as Magento Products on my core PHP site or CodeIgniter site
Is it possible to display wordpress woocommerce products as well as Magento products on my PHP site or CodeIgniter site.
Woocommerce Related Products only showing the same products on reload
Woocommerce version 2.3.10 Wordpress 4.2.2 The related products are cycling through the same products, which seem to be the first few products added to the shop. It displays them in random order, but doesn't randomly show all of the products in the category. I've read that this could be due to a plugin conflict preventing random products from being...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.