|
18 | 18 | use WP_REST_Server; |
19 | 19 | use WC_Product_Variation; |
20 | 20 | use WP_Error; |
| 21 | +use WCPOS\WooCommercePOS\Services\Settings; |
21 | 22 |
|
22 | 23 | /** |
23 | 24 | * Product Tgas controller class. |
@@ -326,15 +327,45 @@ public function wcpos_product_variation_query( array $args, WP_REST_Request $req |
326 | 327 | add_filter( 'posts_groupby', array( $this, 'wcpos_posts_groupby_posts_search' ), 10, 2 ); |
327 | 328 | } |
328 | 329 |
|
| 330 | + // if POS only products are enabled, exclude online-only products |
| 331 | + if ( $this->wcpos_pos_only_products_enabled() ) { |
| 332 | + add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_exclude_online_only' ), 10, 2 ); |
| 333 | + } |
| 334 | + |
329 | 335 | // Check for wcpos_include/wcpos_exclude parameter. |
| 336 | + // NOTE: do this after POS visibility filter so that takes precedence. |
330 | 337 | if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) { |
331 | | - // Add a custom WHERE clause to the query. |
332 | | - add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_include_exclude' ), 10, 2 ); |
| 338 | + add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_include_exclude' ), 20, 2 ); |
333 | 339 | } |
334 | 340 |
|
335 | 341 | return $args; |
336 | 342 | } |
337 | 343 |
|
| 344 | + /** |
| 345 | + * Filters the WHERE clause of the query. |
| 346 | + * |
| 347 | + * @param string $where The WHERE clause of the query. |
| 348 | + * @param WP_Query $query The WP_Query instance (passed by reference). |
| 349 | + * |
| 350 | + * @return string |
| 351 | + */ |
| 352 | + public function wcpos_posts_where_product_variation_exclude_online_only( string $where, WP_Query $query ) { |
| 353 | + global $wpdb; |
| 354 | + |
| 355 | + $settings_instance = Settings::instance(); |
| 356 | + $online_only = $settings_instance->get_online_only_variations_visibility_settings(); |
| 357 | + $online_only_ids = isset( $online_only['ids'] ) && is_array( $online_only['ids'] ) ? $online_only['ids'] : array(); |
| 358 | + |
| 359 | + // Exclude online-only product IDs if POS only products are enabled |
| 360 | + if ( ! empty( $online_only_ids ) ) { |
| 361 | + $online_only_ids = array_map( 'intval', (array) $online_only_ids ); |
| 362 | + $ids_format = implode( ',', array_fill( 0, count( $online_only_ids ), '%d' ) ); |
| 363 | + $where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format) ", $online_only_ids ); |
| 364 | + } |
| 365 | + |
| 366 | + return $where; |
| 367 | + } |
| 368 | + |
338 | 369 | /** |
339 | 370 | * Filters the WHERE clause of the query. |
340 | 371 | * |
@@ -386,13 +417,18 @@ public function wcpos_get_all_posts( $request ) { |
386 | 417 |
|
387 | 418 | // Initialize the SQL query. |
388 | 419 | $sql = "SELECT DISTINCT {$select_fields} FROM {$wpdb->posts}"; |
| 420 | + $sql .= " WHERE {$wpdb->posts}.post_type = 'product_variation' AND {$wpdb->posts}.post_status = 'publish'"; |
389 | 421 |
|
390 | | - // Apply '_pos_visibility' condition if necessary. |
| 422 | + // If the '_pos_visibility' condition needs to be applied. |
391 | 423 | if ( $this->wcpos_pos_only_products_enabled() ) { |
392 | | - $sql .= " LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pos_visibility')"; |
393 | | - $sql .= " WHERE {$wpdb->posts}.post_type = 'product_variation' AND {$wpdb->posts}.post_status = 'publish' AND ({$wpdb->postmeta}.post_id IS NULL OR {$wpdb->postmeta}.meta_value != 'online_only')"; |
394 | | - } else { |
395 | | - $sql .= " WHERE {$wpdb->posts}.post_type = 'product_variation' AND {$wpdb->posts}.post_status = 'publish'"; |
| 424 | + $settings_instance = Settings::instance(); |
| 425 | + $online_only = $settings_instance->get_online_only_variations_visibility_settings(); |
| 426 | + |
| 427 | + if ( isset( $online_only['ids'] ) && is_array( $online_only['ids'] ) && ! empty( $online_only['ids'] ) ) { |
| 428 | + $online_only_ids = array_map( 'intval', (array) $online_only['ids'] ); |
| 429 | + $ids_format = implode( ',', array_fill( 0, count( $online_only_ids ), '%d' ) ); |
| 430 | + $sql .= $wpdb->prepare( " AND ID NOT IN ($ids_format) ", $online_only_ids ); |
| 431 | + } |
396 | 432 | } |
397 | 433 |
|
398 | 434 | // Add modified_after condition if provided. |
@@ -474,26 +510,6 @@ protected function prepare_objects_query( $request ) { |
474 | 510 | } |
475 | 511 | } |
476 | 512 |
|
477 | | - // Add online_only check |
478 | | - if ( $this->wcpos_pos_only_products_enabled() ) { |
479 | | - $meta_query = array( |
480 | | - 'relation' => 'OR', |
481 | | - array( |
482 | | - 'key' => '_pos_visibility', |
483 | | - 'compare' => 'NOT EXISTS', |
484 | | - ), |
485 | | - array( |
486 | | - 'key' => '_pos_visibility', |
487 | | - 'value' => 'online_only', |
488 | | - 'compare' => '!=', |
489 | | - ), |
490 | | - ); |
491 | | - |
492 | | - // Combine meta queries |
493 | | - $args['meta_query'] = $this->wcpos_combine_meta_queries( $args['meta_query'], $meta_query ); |
494 | | - |
495 | | - }; |
496 | | - |
497 | 513 | return $args; |
498 | 514 | } |
499 | 515 |
|
|
0 commit comments