elementFactory = $elementFactory; } public function add_hooks() { Hooks::onFilter( 'woocommerce_product_subcategories_args' ) ->then( spreadArgs( [ $this, 'addFixCountArg' ] ) ); } /** * @param array $args * * @return array */ public function addFixCountArg( $args ) { $this->loadTermsFilter(); return (array) Obj::assoc( self::KEY_FIX_TERM_COUNT_ZERO, true, $args ); } /** * @return void */ private function loadTermsFilter() { if ( ! $this->isTermsFilterLoaded ) { Hooks::onFilter( 'get_terms', 10, 4 ) ->then( spreadArgs( [ $this, 'fixTermsWithZeroCount' ] ) ); $this->isTermsFilterLoaded = true; } } /** * @param array $terms * @param array|null $taxonomy * @param array $termQueryVars * * @return array */ public function fixTermsWithZeroCount( $terms, $taxonomy, $termQueryVars ) { if ( ! empty( $termQueryVars[ self::KEY_FIX_TERM_COUNT_ZERO ] ) ) { foreach ( $terms as $term ) { if ( ! $term->count ) { $term->count = $this->getSourceTermCount( $term ); } } } return $terms; } /** * @param \WP_Term $term * * @return int */ private function getSourceTermCount( $term ) { return (int) Just::of( $this->elementFactory->create_term( $term->term_taxonomy_id ) ) ->map( invoke( 'get_source_element' ) ) ->map( invoke( 'get_wp_object' ) ) ->map( Obj::prop( 'count' ) ) ->getOrElse( $term->count ); } }