skip to Main Content

I am having the following code:

$args = array(
    'limit' => -1,  // Attempt to fetch all products
    'return' => 'ids' // Fetch only IDs to optimize performance; remove this to get full product objects
);

$product_ids = wc_get_products($args);
echo "<pre>";
print_r($product_ids);
echo "</pre>";

This gives the following output:

Array
(
    [0] => 496
    [1] => 106
    [2] => 103
    [3] => 58
)

However, I am having 9 products in WooCommerce.

The following code

function dispProduct($id)
{
    $productArray = array(
        'product_id'       => wc_get_product($id)->get_id(),
        'name'             => wc_get_product($id)->get_name(),
        'sku'              => wc_get_product($id)->get_sku(),
        'price'            => wc_get_product($id)->get_price(),
        'stock_status'     => wc_get_product($id)->get_stock_status(),
        'total_sales'      => wc_get_product($id)->get_total_sales(),
        'regular_price'    => wc_get_product($id)->get_regular_price(),
        'sale_price'       => wc_get_product($id)->get_sale_price(),
        // Add more attributes as needed
    );
    return $productArray;
}


echo "<pre>";
print_r(dispProduct(620));
echo "</pre>";

returns

Array
(
    [product_id] => 620
    [name] => Wordfence Premium
    [sku] => 
    [price] => 1116
    [stock_status] => instock
    [total_sales] => 0
    [regular_price] => 1116
    [sale_price] => 
)

That means, product 620 can be found.

If I go to my products in WooCommerce, click on Quick Edit under product 620, and just hit the "Update" button – without changing anything, then the first code returns

Array
(
    [0] => 620
    [1] => 496
    [2] => 106
    [3] => 103
    [4] => 58
)

But I cannot see any significant differences between 620 and 496 (before or after I update the product using Quick Edit), besides the obvious differences like price, name , sku and so forth.

How can I get ALL the products from WooCommerce?

2

Answers


  1. Chosen as BEST ANSWER

    The solution I found is the following:

    global $wpdb;
    $product_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product' AND post_status = 'publish'");
    
    $products = array();
    foreach ($product_ids as $product_id) {
        $products[] = wc_get_product($product_id);
    }
    

    This returns all the products.


  2. If you’re only getting 4 products instead of the expected 10 when attempting to fetch all products using wc_get_products(), it’s likely due to the default product visibility settings in WooCommerce.

    By default, WooCommerce only includes published products in the query results.

    To ensure that all products, regardless of their publication status, are included in the query results, you can use the status parameter and set it to ‘any’. Here’s how you can modify your $args array:

        $args = array(
        'limit'  => -1,     // Attempt to fetch all products
        'return' => 'ids',  // Fetch only IDs to optimize performance
        'status' => 'any',  // Include products with any status ()
    );
    
    $product_ids = wc_get_products($args);
    echo "<pre>";
    print_r($product_ids);
    echo "</pre>";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search