| @@ -217,8 +217,28 @@ | ||
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | $args = apply_filters( 'booktics_service_args', $args, $request ); |
| 220 | 220 | |
| 221 | + // Get service IDs for category BEFORE pagination to ensure correct total count | |
| 222 | + $service_ids_for_category = null; | |
| 223 | + if ( ! empty( $request['category_id'] ) ) { | |
| 224 | + $service_ids_for_category = get_term_meta( absint( $request['category_id'] ), 'booktics_category_services', true ); | |
| 225 | + | |
| 226 | + // If category filter is applied, add post__in to args to filter BEFORE pagination | |
| 227 | + if ( ! empty( $service_ids_for_category ) && is_array( $service_ids_for_category ) ) { | |
| 228 | + $args['post__in'] = $service_ids_for_category; | |
| 229 | + } elseif ( ! empty( $service_ids_for_category ) ) { | |
| 230 | + // If category has no services, return early | |
| 231 | + return $this->response( array( | |
| 232 | + 'items' => array(), | |
| 233 | + 'total' => 0, | |
| 234 | + 'per_page' => $per_page, | |
| 235 | + 'current_page' => $paged, | |
| 236 | + 'last_page' => 0, | |
| 237 | + ) ); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + | |
| 221 | 241 | $data = Service_Model::paginate( $per_page, $paged, $args ); |
| 222 | 242 | |
| 223 | 243 | if ( ! $data ) { |
| 224 | 244 | return $this->error( __( 'No service found', 'booktics' ), 404 ); |
| @@ -223,20 +243,12 @@ | ||
| 223 | 243 | if ( ! $data ) { |
| 224 | 244 | return $this->error( __( 'No service found', 'booktics' ), 404 ); |
| 225 | 245 | } |
| 226 | 246 | |
| 227 | - if ( ! empty( $request['category_id'] ) ) { | |
| 228 | - $service_ids_for_category = get_term_meta( absint( $request['category_id'] ), 'booktics_category_services', true ); | |
| 229 | - } | |
| 230 | - | |
| 231 | 247 | if ( ! empty( $data['items'] ) ) { |
| 232 | 248 | $enhanced_items = array(); |
| 233 | 249 | foreach ( $data['items'] as $service ) { |
| 234 | 250 | $service_array = $service->to_array(); |
| 235 | - | |
| 236 | - if ( isset( $service_ids_for_category ) && ! in_array( $service_array['id'], $service_ids_for_category ) ) { | |
| 237 | - continue; | |
| 238 | - } | |
| 239 | 251 | |
| 240 | 252 | if ( ! empty( $service_array['team_member'] ) ) { |
| 241 | 253 | $team_member_ids = is_array( $service->team_member ) ? array_unique( $service->team_member ) : array( $service->team_member ); |
| 242 | 254 | $service->team_member = $team_member_ids; |
| @@ -531,9 +543,9 @@ | ||
| 531 | 543 | ++$count; |
| 532 | 544 | } |
| 533 | 545 | } |
| 534 | 546 | |
| 535 | - if ( $count === 0 ) { | |
| 547 | + if ( 0 === $count ) { | |
| 536 | 548 | return $this->error( __( 'No item deleted.', 'booktics' ) ); |
| 537 | 549 | } |
| 538 | 550 | /* translators: 1: deleted count, 2: total count */ |
| 539 | 551 | $message = sprintf( __( 'Deleted %1$d items of %2$d', 'booktics' ), $count, $total ); |