Skip to content

Commit

Permalink
Fixed - Optimize tax queries being added to the main query.
Browse files Browse the repository at this point in the history
  • Loading branch information
twoelevenjay committed Oct 29, 2024
1 parent a1d3e44 commit 372998d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 62 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Changelog

### 2.0.1 10.29.2024
* Fixed - Optimize tax queries being added to the main query.

### 2.0.0 10.27.2024
* Major update to the plugin.
* Fixed - Queries use the taxonomy table for better performance.
Expand Down
163 changes: 103 additions & 60 deletions includes/class-dp-discontinued-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,77 +248,120 @@ public static function add_dp_alt_products() {
}

/**
* Exclude discontinued products.
* Add a the post__not_in argiment to the main query for products.
* Check if the current page is a discontinued product archive page.
*
* @since 1.0.0
* @param object $q Main WP Query.
* @since 2.0.0
* @return bool True if it is a discontinued product archive page, false otherwise.
*/
public function exclude_discontinued_products( $q ) {
if ( $q->is_main_query() ) {
$dc_shop_page_id = (int) get_option( 'dp_shop_page_id' );
$hide_from_shop = get_option( 'dp_hide_from_shop' );
$hide_from_search = get_option( 'dp_hide_from_search' );
public function is_dp_archive_page() {

$dp_discontinued_term = 'dp-discontinued';
$dp_hide_shop_term = 'dp-hide-shop';
$dp_show_shop_term = 'dp-show-shop';
$dp_hide_search_term = 'dp-hide-search';
$dp_show_search_term = 'dp-show-search';
$dc_shop_page_id = (int) get_option( 'dp_shop_page_id' );
return ( is_shop() || is_product_category() ) && $this->current_page_id !== $dc_shop_page_id && ! is_search();
}

$hide_shop_terms = 'yes' === $hide_from_shop ? array( $dp_discontinued_term, $dp_hide_shop_term ) : array( $dp_hide_shop_term );
$hide_search_terms = 'yes' === $hide_from_search ? array( $dp_discontinued_term, $dp_hide_search_term ) : array( $dp_hide_search_term );
/**
* Build the tax query for discontinued products.
*
* @since 2.0.0
* @param WP_Query $q Main WP Query.
* @return array $tax_queries Tax queries for the WP_Query.
*/
public function build_tax_query( $q ) {
$dc_shop_page_id = (int) get_option( 'dp_shop_page_id' );
$hide_from_shop = get_option( 'dp_hide_from_shop' );
$hide_from_search = get_option( 'dp_hide_from_search' );

$tax_query = $q->get( 'tax_query' );
$tax_query = is_array( $tax_query ) ? $tax_query : array();
$tax_queries = $q->get( 'tax_query' );
$tax_queries = is_array( $tax_queries ) ? $tax_queries : array();

if ( $this->current_page_id === $dc_shop_page_id ) {
$tax_queries[] = array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => 'dp-discontinued',
'operator' => 'IN',
);
return $tax_queries;
}

if ( $this->current_page_id === $dc_shop_page_id ) {
$tax_query[] = array(
if ( $this->is_dp_archive_page() && 'yes' === $hide_from_shop ) {
$tax_queries[] = array(
'relation' => 'OR',
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => 'dp-discontinued',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => $dp_discontinued_term,
'terms' => 'dp-show-shop',
'operator' => 'IN',
);
}

if ( ( is_shop() || is_product_category() ) && $this->current_page_id !== $dc_shop_page_id && ! $q->is_search() ) {
$tax_query[] = array(
'relation' => 'OR',
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => $hide_shop_terms,
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => array( $dp_show_shop_term ),
'operator' => 'IN',
),
);
}

if ( $q->is_search() ) {
$tax_query[] = array(
'relation' => 'OR',
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => $hide_search_terms,
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => array( $dp_show_search_term ),
'operator' => 'IN',
),
);
}
),
);
return $tax_queries;
}

$q->set( 'tax_query', $tax_query );
if ( $this->is_dp_archive_page() ) {
$tax_queries[] = array(
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => 'dp-hide-shop',
'operator' => 'NOT IN',
),
);
return $tax_queries;
}

if ( $q->is_search() && 'yes' === $hide_from_search ) {
$tax_queries[] = array(
'relation' => 'OR',
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => 'dp-discontinued',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => 'dp-show-search',
'operator' => 'IN',
),
);
return $tax_queries;
}

if ( $q->is_search() ) {
$tax_queries[] = array(
array(
'taxonomy' => 'product_discontinued',
'field' => 'slug',
'terms' => 'dp-show-search',
'operator' => 'IN',
),
);
return $tax_queries;
}

return $tax_queries;
}

/**
* Exclude discontinued products.
* Add a the post__not_in argiment to the main query for products.
*
* @since 1.0.0
* @param object $q Main WP Query.
*/
public function exclude_discontinued_products( $q ) {
if ( is_admin() || ! $q->is_main_query() ) {
return;
}
$tax_queries = $this->build_tax_query( $q );
$q->set( 'tax_query', $tax_queries );
}


Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**Tags:** woocommerce, woocommerce products, discontinued products, discontinued, ecommerce
**Requires at least:** 6.0
**Tested up to:** 6.6.2
**Stable tag:** 2.0.0
**Stable tag:** 2.0.1
**License:** GPLv2 or later
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html

Expand Down
2 changes: 1 addition & 1 deletion woocommerce-discontinued-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Enables WooCommerce Discontinued Products.
* Author: Leon @ 211J
* Author URI: http://211j.com/
* Version: 2.0.0
* Version: 2.0.1
* Text Domain: discontinued-products
* Domain Path: /languages
Expand Down

0 comments on commit 372998d

Please sign in to comment.