*/ private static $count_cache = array(); /** * @param WP_Term $term Term to sample. * @param array $query_args Extra `wc_get_products()` args (`limit`, * `orderby`, `order`, ...). `return` is * always forced to `'objects'`; `status` * defaults to `array( 'publish' )` and * `visibility` to `'catalog'` when not given, * so products hidden from the catalog never * reach a prompt. When the store hides * out-of-stock products, they are left out too. * @return WC_Product[] */ public static function sample( WP_Term $term, array $query_args = array() ): array { return self::sampleBySlug( (string) $term->taxonomy, (string) $term->slug, $query_args ); } /** * Same as {@see sample()} for a caller that holds a taxonomy name and slug * instead of a term object. * * @param string $taxonomy Taxonomy name. * @param string $slug Term slug. * @param array $query_args Extra `wc_get_products()` args, see {@see sample()}. * @return WC_Product[] */ public static function sampleBySlug( string $taxonomy, string $slug, array $query_args = array() ): array { $query_args['return'] = 'objects'; $products = self::query( $taxonomy, $slug, $query_args ); if ( ! is_array( $products ) ) { return array(); } return array_values( array_filter( $products, static function ( $product ): bool { return $product instanceof WC_Product; } ) ); } /** * How many products the store listing shows for this term: published and * visible in the catalog. `WP_Term::$count` also counts products hidden * from the catalog, so it disagrees with the archive page. Out-of-stock * products are left out when the store hides them. An attribute taxonomy * has no listing of its own and keeps the stored count. Counted with a * one-row paginated query (no ids are loaded) and remembered per term for * the rest of the request. * * @param string $taxonomy Taxonomy name. * @param string $slug Term slug. * @param int $stored_count `WP_Term::$count`, used for an attribute taxonomy. * @return int */ public static function countCatalogProducts( string $taxonomy, string $slug, int $stored_count ): int { if ( 0 === strpos( $taxonomy, 'pa_' ) ) { return $stored_count; } $cache_key = $taxonomy . '|' . $slug . '|' . ( self::hidesOutOfStock() ? '1' : '0' ); if ( ! isset( self::$count_cache[ $cache_key ] ) ) { $result = self::query( $taxonomy, $slug, array( 'limit' => 1, 'paginate' => true, 'return' => 'ids', ) ); self::$count_cache[ $cache_key ] = is_object( $result ) && isset( $result->total ) ? (int) $result->total : 0; } return self::$count_cache[ $cache_key ]; } /** * Forget the remembered counts, for a caller that changes products and * counts again in the same request. * * @return void */ public static function flushCountCache(): void { self::$count_cache = array(); } /** * Whether the store's catalog hides out-of-stock products * (WooCommerce > Settings > Products > Inventory). * * @return bool */ public static function hidesOutOfStock(): bool { return 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ); } /** * @param string $taxonomy Taxonomy name. * @param string $slug Term slug. * @param array $query_args `wc_get_products()` args. * @return array|object Raw `wc_get_products()` result (an object when paginated), empty array when the term cannot be sampled. */ private static function query( string $taxonomy, string $slug, array $query_args ) { if ( '' === $slug || 0 === strpos( $taxonomy, 'pa_' ) || ! function_exists( 'wc_get_products' ) ) { return array(); } if ( 'product_cat' === $taxonomy ) { $query_args['category'] = array( $slug ); } elseif ( 'product_tag' === $taxonomy ) { $query_args['tag'] = array( $slug ); } else { $query_args['tax_query'] = array( array( 'taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => array( $slug ), ), ); } if ( ! isset( $query_args['status'] ) ) { $query_args['status'] = array( 'publish' ); } if ( ! isset( $query_args['visibility'] ) ) { $query_args['visibility'] = 'catalog'; } if ( ! isset( $query_args['stock_status'] ) && self::hidesOutOfStock() ) { $query_args['stock_status'] = array( 'instock', 'onbackorder' ); } $products = wc_get_products( $query_args ); return is_array( $products ) || is_object( $products ) ? $products : array(); } }