| @@ -10,9 +10,17 @@ | ||
| 10 | 10 | { |
| 11 | 11 | use PostMetaTrait; |
| 12 | 12 | |
| 13 | 13 | public $items = []; |
| 14 | + /** | |
| 15 | + * Declared explicitly: assigned from the outside in | |
| 16 | + * EntityManager::prepareEntitiesToDisplay(); dynamic properties are | |
| 17 | + * deprecated since PHP 8.2. | |
| 18 | + */ | |
| 19 | + public $items_sort = []; | |
| 14 | 20 | |
| 21 | + public $filter = []; | |
| 22 | + | |
| 15 | 23 | public $entityName = ''; |
| 16 | 24 | |
| 17 | 25 | public $excludedTerms = []; |
| 18 | 26 | |
| @@ -168,8 +176,38 @@ | ||
| 168 | 176 | if( $position !== false ){ |
| 169 | 177 | unset( $this->items[$index]->posts[$position] ); |
| 170 | 178 | } |
| 171 | 179 | } |
| 180 | + } | |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * Keep only posts that belong to the set universe (all queried posts of | |
| 185 | + * the set), same as TaxonomyEntity does. Without this the raw meta lists | |
| 186 | + * (e.g. _stock_status of ALL published products, including those excluded | |
| 187 | + * from the catalog) leak into wpcFilterJsonData, and the client-side | |
| 188 | + * recount shows inflated counters compared to the server-rendered ones. | |
| 189 | + * | |
| 190 | + * Term posts of "Used for Variations" meta keys legitimately contain | |
| 191 | + * variation IDs which are absent from the parent-product universe, so a | |
| 192 | + * post survives when it is in the universe OR in the universe expanded | |
| 193 | + * to variation IDs. | |
| 194 | + */ | |
| 195 | + $em = Container::instance()->getEntityManager(); | |
| 196 | + $allWpQueriedPostIds = $em->getAllSetWpQueriedPostIds( $setId ); | |
| 197 | + | |
| 198 | + if ( ! empty( $allWpQueriedPostIds ) ) { | |
| 199 | + $universe = array_flip( $allWpQueriedPostIds ); | |
| 200 | + $universe_variations = apply_filters( 'wpc_from_products_to_variations', $universe ); | |
| 201 | + | |
| 202 | + foreach ( $this->items as $index => $term ) { | |
| 203 | + $intersected_posts = []; | |
| 204 | + foreach ( $term->posts as $post_id ) { | |
| 205 | + if ( isset( $universe[ $post_id ] ) || isset( $universe_variations[ $post_id ] ) ) { | |
| 206 | + $intersected_posts[] = $post_id; | |
| 207 | + } | |
| 208 | + } | |
| 209 | + $this->items[ $index ]->posts = $intersected_posts; | |
| 172 | 210 | } |
| 173 | 211 | } |
| 174 | 212 | } |
| 175 | 213 | |