Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Shopify
ShopifyFetcher::fetch_total_count
Fetches the total count of products from the Shopify REST API.
Method of the class: ShopifyFetcher{}
No Hooks.
Returns
Int. The total count, or 0 on failure.
Usage
$ShopifyFetcher = new ShopifyFetcher(); $ShopifyFetcher->fetch_total_count( $args ): int;
- $args(array) (required)
- Arguments for filtering the count (e.g., status, date range).
ShopifyFetcher::fetch_total_count() ShopifyFetcher::fetch total count code WC 10.7.0
public function fetch_total_count( array $args ): int {
// Handle special case: if specific IDs are provided, count them directly.
if ( isset( $args['ids'] ) ) {
\WP_CLI::debug( 'Calculating total count based on provided product IDs.' );
$ids = is_array( $args['ids'] ) ? $args['ids'] : explode( ',', $args['ids'] );
return count( array_filter( $ids ) );
}
$rest_api_path = '/products/count.json';
$query_params = $this->build_count_query_params( $args );
$response = $this->shopify_client->rest_request( $rest_api_path, $query_params );
if ( is_wp_error( $response ) ) {
\WP_CLI::warning( 'Could not fetch total product count from Shopify REST API: ' . $response->get_error_message() );
return 0;
}
if ( ! isset( $response->count ) ) {
\WP_CLI::warning( 'Unexpected response format from Shopify count API - missing count field.' );
return 0;
}
return (int) $response->count;
}