WooCommerce product query default vars

/**
 * WooCommerce product query (`wc_get_products()`) default vars.
 *
 * @see https://wpcodebook.com/snippets/woocommerce-product-query-default-vars/
 * @see https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
 * @see https://github.com/woocommerce/woocommerce/blob/7.9.0/plugins/woocommerce/includes/class-wc-product-query.php#L24
 */
$default_query_vars = array(
	'status'            => array( 'draft', 'pending', 'private', 'publish' ),
	'type'              => array_merge( array_keys( wc_get_product_types() ) ),
	'limit'             => get_option( 'posts_per_page' ),
	'include'           => array(),
	'date_created'      => '',
	'date_modified'     => '',
	'featured'          => '',
	'visibility'        => '',
	'sku'               => '',
	'price'             => '',
	'regular_price'     => '',
	'sale_price'        => '',
	'date_on_sale_from' => '',
	'date_on_sale_to'   => '',
	'total_sales'       => '',
	'tax_status'        => '',
	'tax_class'         => '',
	'manage_stock'      => '',
	'stock_quantity'    => '',
	'stock_status'      => '',
	'backorders'        => '',
	'low_stock_amount'  => '',
	'sold_individually' => '',
	'weight'            => '',
	'length'            => '',
	'width'             => '',
	'height'            => '',
	'reviews_allowed'   => '',
	'virtual'           => '',
	'downloadable'      => '',
	'category'          => array(),
	'tag'               => array(),
	'shipping_class'    => array(),
	'download_limit'    => '',
	'download_expiry'   => '',
	'average_rating'    => '',
	'review_count'      => '',
);

/**
 * Example.
 */
foreach ( wc_get_products( $default_query_vars ) as $product ) {
	$product->set_price( 10.0 );
	$product->save();
}

Leave a Comment