Cannot get all products in PHP using wc_get_products
-
I am creating a new plugin to WordPress, that uses data from WooCommerce.
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 should display all the products I am having in my WooCommerce.
However, it only returns four of them.
I am also having 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>";
This displays some data from product 620. However, 620 is not in the list, that displays my products, even though the product clearly exists.
When looking in WooCommerce, I can see I am having 9 products.
If I quick-edit one of those, that are not in the $product_ids array, and just hit the “Update” button, without editing something, the product will show up in the array, after a refresh.
Anyone having an idea?The page I need help with: [log in to see the link]
- The topic ‘Cannot get all products in PHP using wc_get_products’ is closed to new replies.