Get all WooCommerce products and variations with PHP

/**
 * Gets all WooCommerce products and variations.
 *
 * @see https://wpcodebook.com/snippets/get-all-woocommerce-products-and-variations-with-php/
 * @see https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
 */
$products = wc_get_products( array( 
	'limit' => -1, 
	'type'  => array_merge( array_keys( wc_get_product_types() ), array( 'variation' ) ),
) );

foreach ( $products as $product ) {
	// do stuff here...
}

By default, the wc_get_product_types() function will return an array with these keys:

array( 'simple', 'grouped', 'external', 'variable' )

Leave a Comment