Mostafa Ghanbari
Mostafa Ghanbari

Reputation: 374

Get Products by Category id

i am writing a pricing table plugin for woocommerce. the user inserts the shortcode with an id of woocommerce products category and after updating the page the user can see a table with a list of product's names and prices.
how can i get a list of products with the id of their category?!
in the code below $pid is what the user type in shortcode, 'object_id' is the id of the every product in wp_posts table.

<?php
    $products = $wpdb->get_results("SELECT object_id FROM {$wpdb->term_relationships}
                                      WHERE term_taxonomy_id = " . $pid);
    if(!empty($products))
    {
        foreach($products as $product)
        {
            //the code
        }
    }
?>  

Thanks in advance.

Upvotes: 6

Views: 48023

Answers (6)

vorwerg-ni
vorwerg-ni

Reputation: 177

I am using this code and it works well:

$category_id = 42;
$products = wc_get_products(array(
    'limit' => -1,
    'tax_query'             => array(
        array(
            'taxonomy'      => 'product_cat',
            'field'         => 'term_id',
            'terms'         => $category_id,
            'operator'      => 'IN'
        ),
    ),
));
foreach($products as $product)
{
    echo $product->get_title();
}

Upvotes: 1

Yogesh
Yogesh

Reputation: 1

$posts = get_posts(
    array(
       'post_type'   => 'product',
        'numberposts' => -1,
        'post_status' => 'publish',
        'fields'      => 'ids',
        'tax_query'   => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => 'books-and-handbooks',
                'operator' => 'IN',
            ),
        ),
    )
);
$arr = array_slice( $all_ids, 0, 50 );

Upvotes: 0

Prince Singh
Prince Singh

Reputation: 5183

 $args = array(
    'post_status' => 'publish',
    'tax_query' => array(
       array(
         'taxonomy' => 'product_cat',
         'field'    => 'term_id',
         'terms'     =>  '[ category id here ]', // When you have more term_id's seperate them by komma.
         'operator'  => 'IN'
         )
       )
    );
    $the_query = wp_query($args);

Untested but should work

Upvotes: 11

J Grandjean
J Grandjean

Reputation: 101

According to Woocommerce documentation WP_Query() or get_posts() should not be used:

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. This is the best-practices way for plugin and theme developers to retrieve multiple products. wc_get_products and WC_Product_Query are similar to WordPress get_posts and WP_Query. Just like those, you pass in an array of arguments defining the criteria for the search.

WooCommerce Docs

Here's my solution:

$product_term_ids = array(16,10,4,7);

$product_term_args = array(
    'taxonomy' => 'product_cat',
    'include' => $product_term_ids,
    'orderby'  => 'include'
);
$product_terms = get_terms($product_term_args);

$product_term_slugs = [];
foreach ($product_terms as $product_term) {
    $product_term_slugs[] = $product_term->slug;
}

$product_args = array(
    'post_status' => 'publish',
    'limit' => -1,
    'category' => $product_term_slugs,
    //more options according to wc_get_products() docs
);
$products = wc_get_products($product_args);

foreach ($products as $product) {
    echo $product->get_title();
}

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

(Tested and works with Wordpress 5.9.3 & WooCommerce 6.4.1.)

Credit: @Christian Lescuyer

Upvotes: 9

Rahul Shinde
Rahul Shinde

Reputation: 1662

By using the get_posts wordpress function

Get All WooCommerce Product Details by Category

$all_products = get_posts( array(
    'post_type' => 'product',
    'numberposts' => -1,
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'your_product_category', /*category name*/
            'operator' => 'IN',
            )
        ),
    ));
    echo var_dump($all_products);

Get All WooCommerce Product IDs by Category

$all_ids = get_posts( array(
  'post_type' => 'product',
  'numberposts' => -1,
  'post_status' => 'publish',
  'fields' => 'ids',
  'tax_query' => array(
      array(
          'taxonomy' => 'product_cat',
          'field' => 'slug',
          'terms' => 'your_product_category', /*category name*/
          'operator' => 'IN',
          )
       ),
   ));
   foreach ( $all_ids as $id ) {
       echo $id;
   }

Tested and its working fine.

Upvotes: 7

zak
zak

Reputation: 876

The above (accepted) answer didn't work (more results than there should have been) for me (version 4.9.8)

 'tax_query' => array(
     array(
        'taxonomy' => 'product_cat',
        'field'    => 'term_id',
        'terms'     =>  [$request['id']],
        'operator'  => 'IN'
      )
  )

The above works. I'm not really a Wordpress person but I'd guess it's a version thing...

Upvotes: 1

Related Questions