Skip to content

Commit 8fd22b8

Browse files
committed
change pos visibility settings
1 parent 40df182 commit 8fd22b8

File tree

12 files changed

+746
-188
lines changed

12 files changed

+746
-188
lines changed

includes/API/Product_Variations_Controller.php

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use WP_REST_Server;
1919
use WC_Product_Variation;
2020
use WP_Error;
21+
use WCPOS\WooCommercePOS\Services\Settings;
2122

2223
/**
2324
* Product Tgas controller class.
@@ -326,15 +327,45 @@ public function wcpos_product_variation_query( array $args, WP_REST_Request $req
326327
add_filter( 'posts_groupby', array( $this, 'wcpos_posts_groupby_posts_search' ), 10, 2 );
327328
}
328329

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+
329335
// Check for wcpos_include/wcpos_exclude parameter.
336+
// NOTE: do this after POS visibility filter so that takes precedence.
330337
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 );
333339
}
334340

335341
return $args;
336342
}
337343

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+
338369
/**
339370
* Filters the WHERE clause of the query.
340371
*
@@ -386,13 +417,18 @@ public function wcpos_get_all_posts( $request ) {
386417

387418
// Initialize the SQL query.
388419
$sql = "SELECT DISTINCT {$select_fields} FROM {$wpdb->posts}";
420+
$sql .= " WHERE {$wpdb->posts}.post_type = 'product_variation' AND {$wpdb->posts}.post_status = 'publish'";
389421

390-
// Apply '_pos_visibility' condition if necessary.
422+
// If the '_pos_visibility' condition needs to be applied.
391423
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+
}
396432
}
397433

398434
// Add modified_after condition if provided.
@@ -474,26 +510,6 @@ protected function prepare_objects_query( $request ) {
474510
}
475511
}
476512

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-
497513
return $args;
498514
}
499515

includes/API/Products_Controller.php

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use WP_REST_Request;
1919
use WP_REST_Response;
2020
use WP_Error;
21+
use WCPOS\WooCommercePOS\Services\Settings;
2122

2223
/**
2324
* Products controller class.
@@ -352,10 +353,14 @@ public function wcpos_product_query( array $args, WP_REST_Request $request ) {
352353
add_filter( 'posts_groupby', array( $this, 'wcpos_posts_groupby_product_search' ), 10, 2 );
353354
}
354355

356+
// if POS only products are enabled, exclude online-only products
357+
if ( $this->wcpos_pos_only_products_enabled() ) {
358+
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_exclude_online_only' ), 10, 2 );
359+
}
360+
355361
// Check for wcpos_include/wcpos_exclude parameter.
356362
if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) {
357-
// Add a custom WHERE clause to the query.
358-
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_include_exclude' ), 10, 2 );
363+
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_include_exclude' ), 20, 2 );
359364
}
360365

361366
return $args;
@@ -397,6 +402,31 @@ public function wcpos_posts_groupby_product_search( string $groupby, WP_Query $q
397402
return $groupby;
398403
}
399404

405+
/**
406+
* Filters the WHERE clause of the query.
407+
*
408+
* @param string $where The WHERE clause of the query.
409+
* @param WP_Query $query The WP_Query instance (passed by reference).
410+
*
411+
* @return string
412+
*/
413+
public function wcpos_posts_where_product_exclude_online_only( string $where, WP_Query $query ) {
414+
global $wpdb;
415+
416+
$settings_instance = Settings::instance();
417+
$online_only = $settings_instance->get_online_only_product_visibility_settings();
418+
$online_only_ids = isset( $online_only['ids'] ) && is_array( $online_only['ids'] ) ? $online_only['ids'] : array();
419+
420+
// Exclude online-only product IDs if POS only products are enabled
421+
if ( ! empty( $online_only_ids ) ) {
422+
$online_only_ids = array_map( 'intval', (array) $online_only_ids );
423+
$ids_format = implode( ',', array_fill( 0, count( $online_only_ids ), '%d' ) );
424+
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format) ", $online_only_ids );
425+
}
426+
427+
return $where;
428+
}
429+
400430
/**
401431
* Filters the WHERE clause of the query.
402432
*
@@ -447,13 +477,17 @@ public function wcpos_get_all_posts( $request ) {
447477

448478
// Use SELECT DISTINCT in the initial SQL statement for both cases.
449479
$sql = "SELECT DISTINCT {$select_fields} FROM {$wpdb->posts}";
480+
$sql .= " WHERE post_type = 'product' AND post_status = 'publish'";
450481

451482
// If the '_pos_visibility' condition needs to be applied.
452483
if ( $this->wcpos_pos_only_products_enabled() ) {
453-
$sql .= " LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pos_visibility')";
454-
$sql .= " WHERE post_type = 'product' AND post_status = 'publish' AND ({$wpdb->postmeta}.post_id IS NULL OR {$wpdb->postmeta}.meta_value != 'online_only')";
455-
} else {
456-
$sql .= " WHERE post_type = 'product' AND post_status = 'publish'";
484+
$settings_instance = Settings::instance();
485+
$online_only = $settings_instance->get_online_only_product_visibility_settings();
486+
if ( isset( $online_only['ids'] ) && is_array( $online_only['ids'] ) && ! empty( $online_only['ids'] ) ) {
487+
$online_only_ids = array_map( 'intval', (array) $online_only['ids'] );
488+
$ids_format = implode( ',', array_fill( 0, count( $online_only_ids ), '%d' ) );
489+
$sql .= $wpdb->prepare( " AND ID NOT IN ($ids_format) ", $online_only_ids );
490+
}
457491
}
458492

459493
// Add modified_after condition if provided.
@@ -530,24 +564,6 @@ protected function prepare_objects_query( $request ) {
530564
}
531565
}
532566

533-
// Add online_only check
534-
if ( $this->wcpos_pos_only_products_enabled() ) {
535-
$meta_query = array(
536-
'relation' => 'OR',
537-
array(
538-
'key' => '_pos_visibility',
539-
'compare' => 'NOT EXISTS',
540-
),
541-
array(
542-
'key' => '_pos_visibility',
543-
'value' => 'online_only',
544-
'compare' => '!=',
545-
),
546-
);
547-
548-
$args['meta_query'] = $this->wcpos_combine_meta_queries( $meta_query, $args['meta_query'] );
549-
};
550-
551567
return $args;
552568
}
553569
}

includes/Activator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ private function db_upgrade( $old, $current ): void {
252252
'0.4' => 'updates/update-0.4.php',
253253
'0.4.6' => 'updates/update-0.4.6.php',
254254
'1.0.0-beta.1' => 'updates/update-1.0.0-beta.1.php',
255+
'1.6.1' => 'updates/update-1.6.1.php',
255256
);
256257
foreach ( $db_updates as $version => $updater ) {
257258
if ( version_compare( $version, $old, '>' ) &&

0 commit comments

Comments
 (0)