Skip to content

Commit

Permalink
change pos visibility settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jun 17, 2024
1 parent 40df182 commit 8fd22b8
Show file tree
Hide file tree
Showing 12 changed files with 746 additions and 188 deletions.
70 changes: 43 additions & 27 deletions includes/API/Product_Variations_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use WP_REST_Server;
use WC_Product_Variation;
use WP_Error;
use WCPOS\WooCommercePOS\Services\Settings;

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

// if POS only products are enabled, exclude online-only products
if ( $this->wcpos_pos_only_products_enabled() ) {
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_exclude_online_only' ), 10, 2 );
}

// Check for wcpos_include/wcpos_exclude parameter.
// NOTE: do this after POS visibility filter so that takes precedence.
if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) {
// Add a custom WHERE clause to the query.
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_include_exclude' ), 10, 2 );
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_include_exclude' ), 20, 2 );
}

return $args;
}

/**
* Filters the WHERE clause of the query.
*
* @param string $where The WHERE clause of the query.
* @param WP_Query $query The WP_Query instance (passed by reference).
*
* @return string
*/
public function wcpos_posts_where_product_variation_exclude_online_only( string $where, WP_Query $query ) {
global $wpdb;

$settings_instance = Settings::instance();
$online_only = $settings_instance->get_online_only_variations_visibility_settings();
$online_only_ids = isset( $online_only['ids'] ) && is_array( $online_only['ids'] ) ? $online_only['ids'] : array();

// Exclude online-only product IDs if POS only products are enabled
if ( ! empty( $online_only_ids ) ) {
$online_only_ids = array_map( 'intval', (array) $online_only_ids );
$ids_format = implode( ',', array_fill( 0, count( $online_only_ids ), '%d' ) );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format) ", $online_only_ids );
}

return $where;
}

/**
* Filters the WHERE clause of the query.
*
Expand Down Expand Up @@ -386,13 +417,18 @@ public function wcpos_get_all_posts( $request ) {

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

// Apply '_pos_visibility' condition if necessary.
// If the '_pos_visibility' condition needs to be applied.
if ( $this->wcpos_pos_only_products_enabled() ) {
$sql .= " LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pos_visibility')";
$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')";
} else {
$sql .= " WHERE {$wpdb->posts}.post_type = 'product_variation' AND {$wpdb->posts}.post_status = 'publish'";
$settings_instance = Settings::instance();
$online_only = $settings_instance->get_online_only_variations_visibility_settings();

if ( isset( $online_only['ids'] ) && is_array( $online_only['ids'] ) && ! empty( $online_only['ids'] ) ) {
$online_only_ids = array_map( 'intval', (array) $online_only['ids'] );
$ids_format = implode( ',', array_fill( 0, count( $online_only_ids ), '%d' ) );
$sql .= $wpdb->prepare( " AND ID NOT IN ($ids_format) ", $online_only_ids );
}
}

// Add modified_after condition if provided.
Expand Down Expand Up @@ -474,26 +510,6 @@ protected function prepare_objects_query( $request ) {
}
}

// Add online_only check
if ( $this->wcpos_pos_only_products_enabled() ) {
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_pos_visibility',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_pos_visibility',
'value' => 'online_only',
'compare' => '!=',
),
);

// Combine meta queries
$args['meta_query'] = $this->wcpos_combine_meta_queries( $args['meta_query'], $meta_query );

};

return $args;
}

Expand Down
64 changes: 40 additions & 24 deletions includes/API/Products_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use WCPOS\WooCommercePOS\Services\Settings;

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

// if POS only products are enabled, exclude online-only products
if ( $this->wcpos_pos_only_products_enabled() ) {
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_exclude_online_only' ), 10, 2 );
}

// Check for wcpos_include/wcpos_exclude parameter.
if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) {
// Add a custom WHERE clause to the query.
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_include_exclude' ), 10, 2 );
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_include_exclude' ), 20, 2 );
}

return $args;
Expand Down Expand Up @@ -397,6 +402,31 @@ public function wcpos_posts_groupby_product_search( string $groupby, WP_Query $q
return $groupby;
}

/**
* Filters the WHERE clause of the query.
*
* @param string $where The WHERE clause of the query.
* @param WP_Query $query The WP_Query instance (passed by reference).
*
* @return string
*/
public function wcpos_posts_where_product_exclude_online_only( string $where, WP_Query $query ) {
global $wpdb;

$settings_instance = Settings::instance();
$online_only = $settings_instance->get_online_only_product_visibility_settings();
$online_only_ids = isset( $online_only['ids'] ) && is_array( $online_only['ids'] ) ? $online_only['ids'] : array();

// Exclude online-only product IDs if POS only products are enabled
if ( ! empty( $online_only_ids ) ) {
$online_only_ids = array_map( 'intval', (array) $online_only_ids );
$ids_format = implode( ',', array_fill( 0, count( $online_only_ids ), '%d' ) );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format) ", $online_only_ids );
}

return $where;
}

/**
* Filters the WHERE clause of the query.
*
Expand Down Expand Up @@ -447,13 +477,17 @@ public function wcpos_get_all_posts( $request ) {

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

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

// Add modified_after condition if provided.
Expand Down Expand Up @@ -530,24 +564,6 @@ protected function prepare_objects_query( $request ) {
}
}

// Add online_only check
if ( $this->wcpos_pos_only_products_enabled() ) {
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_pos_visibility',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_pos_visibility',
'value' => 'online_only',
'compare' => '!=',
),
);

$args['meta_query'] = $this->wcpos_combine_meta_queries( $meta_query, $args['meta_query'] );
};

return $args;
}
}
1 change: 1 addition & 0 deletions includes/Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ private function db_upgrade( $old, $current ): void {
'0.4' => 'updates/update-0.4.php',
'0.4.6' => 'updates/update-0.4.6.php',
'1.0.0-beta.1' => 'updates/update-1.0.0-beta.1.php',
'1.6.1' => 'updates/update-1.6.1.php',
);
foreach ( $db_updates as $version => $updater ) {
if ( version_compare( $version, $old, '>' ) &&
Expand Down
Loading

0 comments on commit 8fd22b8

Please sign in to comment.