Get all WooCommerce products sorted by price

/**
 * Gets all WooCommerce products sorted by price.
 *
 * @see https://wpcodebook.com/woocommerce-get-products-sorted-price/
 * @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_num',
	'meta_key' => '_price',
) );

foreach ( $products as $product ) {
	echo $product->get_name() . ' - ' . wc_price( $product->get_price() ) . '<br>';
}

Leave a Comment