When you work with WooCommerce PHP snippets, you often need to “get” stuff off your WordPress database in order to make some calculations and return the result on the screen. And if there is something I often google is “How do I get all my store’s product IDs?“.

Thankfully the get_posts WordPress function gives us the answer. In the next weeks we’ll also see how we can get other information, such as all SKUs, all product categories, etc.

So, here’s the quick snippet to return all product IDs on the Cart page (“woocommerce_before_cart” hook) – you can change that if you want to use them somewhere else. Enjoy 🙂

WooCommerce: get all product IDs

PHP Snippet 1: Get All WooCommerce Product IDs

/**
 * @snippet       Get / Return WooCommerce Product IDs
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_before_cart', 'bbloomer_echo_product_ids' );

function bbloomer_echo_product_ids() {
   $all_ids = get_posts( array(
        'post_type' => 'product',
        'numberposts' => -1,
        'post_status' => 'publish',
        'fields' => 'ids',
   ) );
   foreach ( $all_ids as $id ) {
        echo $id;
   }
}

PHP Snippet 2: Get All WooCommerce Product IDs by Product Tag

/**
 * @snippet       Get / Return WooCommerce Product IDs by Tag
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_before_cart', 'bbloomer_echo_product_ids_belong_to_tag' );

function bbloomer_echo_product_ids_belong_to_tag() {
   $all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
         array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => 'your_product_tag',
            'operator' => 'IN',
         )
      ),
   ) );
   foreach ( $all_ids as $id ) {
      echo $id;
   }
}

PHP Snippet 3: Get All WooCommerce Product IDs by Product Category

/**
 * @snippet       Get / Return WooCommerce Product IDs by Cat
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_before_cart', 'bbloomer_echo_product_ids_belong_to_cat' );

function bbloomer_echo_product_ids_belong_to_cat() {
   $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_cat',
            'operator' => 'IN',
         )
      ),
   ) );
   foreach ( $all_ids as $id ) {
      echo $id;
   }
}
Share:

Leave a Reply