Get all WooCommerce products sorted by SKU

/**
 * Gets all WooCommerce products sorted by SKU.
 *
 * @see https://wpcodebook.com/get-all-woocommerce-products-sorted-by-sku/
 * @see https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
 * @see https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
 */
$products = wc_get_products( array(
	'limit'    => -1,
	'order'    => 'ASC',
	'orderby'  => 'meta_value',
	'meta_key' => '_sku',
) );

foreach ( $products as $product ) {
	printf( '%s (%s)<br>', $product->get_name(), $product->get_sku() );
}

Please be aware that products without an SKU will be excluded.

Leave a Comment