From 35819ccb6e3fb5798fc1b26f6935b16342b2e40d Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 10:53:48 +0000 Subject: [PATCH 01/14] Small refactor for clarity --- includes/wc-core-functions.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index b45a15fa6dc..60c3488c1cc 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -159,19 +159,28 @@ function wc_update_order( $args ) { function wc_get_template_part( $slug, $name = '' ) { $template = ''; - // Look in yourtheme/slug-name.php and yourtheme/woocommerce/slug-name.php. - if ( $name && ! WC_TEMPLATE_DEBUG_MODE ) { - $template = locate_template( array( "{$slug}-{$name}.php", WC()->template_path() . "{$slug}-{$name}.php" ) ); + if ( $name ) { + $template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template( + array( + "{$slug}-{$name}.php", + WC()->template_path() . "{$slug}-{$name}.php", + ) + ); + + if ( ! $template ) { + $fallback = WC()->plugin_path() . "/templates/{$slug}-{$name}.php"; + $template = file_exists( $fallback ) ? $fallback : ''; + } } - // Get default slug-name.php. - if ( ! $template && $name && file_exists( WC()->plugin_path() . "/templates/{$slug}-{$name}.php" ) ) { - $template = WC()->plugin_path() . "/templates/{$slug}-{$name}.php"; - } - - // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php. - if ( ! $template && ! WC_TEMPLATE_DEBUG_MODE ) { - $template = locate_template( array( "{$slug}.php", WC()->template_path() . "{$slug}.php" ) ); + if ( ! $template ) { + // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php. + $template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template( + array( + "{$slug}.php", + WC()->template_path() . "{$slug}.php", + ) + ); } // Allow 3rd party plugins to filter template file from their plugin. From 6fa7995f3a9658225f47c346da2b2d4f9f814b79 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 10:54:02 +0000 Subject: [PATCH 02/14] Only check file exists if filtered --- includes/wc-core-functions.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 60c3488c1cc..234811c019e 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -203,12 +203,15 @@ function wc_get_template( $template_name, $args = array(), $template_path = '', $located = wc_locate_template( $template_name, $template_path, $default_path ); // Allow 3rd party plugin filter template file from their plugin. - $located = apply_filters( 'wc_get_template', $located, $template_name, $args, $template_path, $default_path ); + $filter_located = apply_filters( 'wc_get_template', $located, $template_name, $args, $template_path, $default_path ); - if ( ! file_exists( $located ) ) { - /* translators: %s template */ - wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'woocommerce' ), '' . $located . '' ), '2.1' ); - return; + if ( $filter_located !== $located ) { + if ( ! file_exists( $filter_located ) ) { + /* translators: %s template */ + wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'woocommerce' ), '' . $located . '' ), '2.1' ); + return; + } + $located = $filter_located; } $action_args = array( From d167cb0a9593b8038f510435aa1bd706b7d762cd Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 12:55:59 +0000 Subject: [PATCH 03/14] Add caching for template names to avoid multiple lookups --- includes/wc-core-functions.php | 71 +++++++++++++++++------------- includes/wc-template-functions.php | 2 + 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 234811c019e..3460d67dd63 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -157,30 +157,35 @@ function wc_update_order( $args ) { * @param string $name Template name (default: ''). */ function wc_get_template_part( $slug, $name = '' ) { - $template = ''; - - if ( $name ) { - $template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template( - array( - "{$slug}-{$name}.php", - WC()->template_path() . "{$slug}-{$name}.php", - ) - ); - - if ( ! $template ) { - $fallback = WC()->plugin_path() . "/templates/{$slug}-{$name}.php"; - $template = file_exists( $fallback ) ? $fallback : ''; - } - } + $cache_key = sanitize_key( implode( '-', array( 'template-part', $slug, $name ) ) ); + $template = (string) wp_cache_get( $cache_key, 'woocommerce' ); if ( ! $template ) { - // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php. - $template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template( - array( - "{$slug}.php", - WC()->template_path() . "{$slug}.php", - ) - ); + if ( $name ) { + $template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template( + array( + "{$slug}-{$name}.php", + WC()->template_path() . "{$slug}-{$name}.php", + ) + ); + + if ( ! $template ) { + $fallback = WC()->plugin_path() . "/templates/{$slug}-{$name}.php"; + $template = file_exists( $fallback ) ? $fallback : ''; + } + } + + if ( ! $template ) { + // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/woocommerce/slug.php. + $template = WC_TEMPLATE_DEBUG_MODE ? '' : locate_template( + array( + "{$slug}.php", + WC()->template_path() . "{$slug}.php", + ) + ); + } + + wp_cache_set( $cache_key, $template, 'woocommerce' ); } // Allow 3rd party plugins to filter template file from their plugin. @@ -200,24 +205,30 @@ function wc_get_template_part( $slug, $name = '' ) { * @param string $default_path Default path. (default: ''). */ function wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { - $located = wc_locate_template( $template_name, $template_path, $default_path ); + $cache_key = sanitize_key( implode( '-', array( 'template', $template_name, $template_path, $default_path ) ) ); + $template = (string) wp_cache_get( $cache_key, 'woocommerce' ); + + if ( ! $template ) { + $template = wc_locate_template( $template_name, $template_path, $default_path ); + wp_cache_set( $cache_key, $template, 'woocommerce' ); + } // Allow 3rd party plugin filter template file from their plugin. - $filter_located = apply_filters( 'wc_get_template', $located, $template_name, $args, $template_path, $default_path ); + $filter_template = apply_filters( 'wc_get_template', $template, $template_name, $args, $template_path, $default_path ); - if ( $filter_located !== $located ) { - if ( ! file_exists( $filter_located ) ) { + if ( $filter_template !== $template ) { + if ( ! file_exists( $filter_template ) ) { /* translators: %s template */ - wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'woocommerce' ), '' . $located . '' ), '2.1' ); + wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%s does not exist.', 'woocommerce' ), '' . $template . '' ), '2.1' ); return; } - $located = $filter_located; + $template = $filter_template; } $action_args = array( 'template_name' => $template_name, 'template_path' => $template_path, - 'located' => $located, + 'located' => $template, 'args' => $args, ); @@ -227,7 +238,7 @@ function wc_get_template( $template_name, $args = array(), $template_path = '', do_action( 'woocommerce_before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] ); - include $located; + include $template; do_action( 'woocommerce_after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] ); } diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index e2e08da9684..204991b9db3 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -398,6 +398,8 @@ function wc_reset_product_grid_settings() { if ( ! empty( $product_grid['default_columns'] ) ) { update_option( 'woocommerce_catalog_columns', absint( $product_grid['default_columns'] ) ); } + + wp_cache_flush(); // Flush any caches which could impact settings or templates. } add_action( 'after_switch_theme', 'wc_reset_product_grid_settings' ); From 536d71a5f68ba4013240e2d57338dd02faa81c74 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 13:11:09 +0000 Subject: [PATCH 04/14] cache get_current_page_url --- includes/abstracts/abstract-wc-widget.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/includes/abstracts/abstract-wc-widget.php b/includes/abstracts/abstract-wc-widget.php index eb91c48d081..50961c40d4c 100644 --- a/includes/abstracts/abstract-wc-widget.php +++ b/includes/abstracts/abstract-wc-widget.php @@ -289,6 +289,13 @@ abstract class WC_Widget extends WP_Widget { * @since 3.3.0 */ protected function get_current_page_url() { + $cache_key = 'woocommerce_widget_get_current_page_url'; + $cached_value = (string) wp_cache_get( $cache_key, 'woocommerce' ); + + if ( $cached_value ) { + return apply_filters( 'woocommerce_widget_get_current_page_url', $cached_value, $this ); + } + if ( defined( 'SHOP_IS_ON_FRONT' ) ) { $link = home_url(); } elseif ( is_shop() ) { From fa58f3bb9fb9b36c466d6072e0102ae31d2c2434 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 13:21:11 +0000 Subject: [PATCH 05/14] Lax the kses here - link and name is already escaped. --- includes/widgets/class-wc-widget-layered-nav.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/widgets/class-wc-widget-layered-nav.php b/includes/widgets/class-wc-widget-layered-nav.php index 66b9246a334..b51d1871fc7 100644 --- a/includes/widgets/class-wc-widget-layered-nav.php +++ b/includes/widgets/class-wc-widget-layered-nav.php @@ -458,8 +458,8 @@ class WC_Widget_Layered_Nav extends WC_Widget { } if ( $count > 0 || $option_is_set ) { - $link = esc_url( apply_filters( 'woocommerce_layered_nav_link', $link, $term, $taxonomy ) ); - $term_html = '' . esc_html( $term->name ) . ''; + $link = apply_filters( 'woocommerce_layered_nav_link', $link, $term, $taxonomy ); + $term_html = '' . esc_html( $term->name ) . ''; } else { $link = false; $term_html = '' . esc_html( $term->name ) . ''; @@ -468,7 +468,7 @@ class WC_Widget_Layered_Nav extends WC_Widget { $term_html .= ' ' . apply_filters( 'woocommerce_layered_nav_count', '(' . absint( $count ) . ')', $count, $term ); echo '
  • '; - echo wp_kses_post( apply_filters( 'woocommerce_layered_nav_term_html', $term_html, $term, $link, $count ) ); + echo apply_filters( 'woocommerce_layered_nav_term_html', $term_html, $term, $link, $count ); // WPCS: XSS ok. echo '
  • '; } From a5bc236c66c9f55fdbdc6bf777f185e11be37200 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 13:23:49 +0000 Subject: [PATCH 06/14] get_current_page_url cache set --- includes/abstracts/abstract-wc-widget.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/abstracts/abstract-wc-widget.php b/includes/abstracts/abstract-wc-widget.php index 50961c40d4c..74adb9e18f3 100644 --- a/includes/abstracts/abstract-wc-widget.php +++ b/includes/abstracts/abstract-wc-widget.php @@ -354,6 +354,8 @@ abstract class WC_Widget extends WP_Widget { } } + wp_cache_set( $cache_key, $link, 'woocommerce' ); + return apply_filters( 'woocommerce_widget_get_current_page_url', $link, $this ); } From 4622af890ade56a41028ac026c619a85c2908f25 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 13:27:48 +0000 Subject: [PATCH 07/14] wc_attribute_taxonomy_slug cache --- includes/wc-attribute-functions.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/wc-attribute-functions.php b/includes/wc-attribute-functions.php index 6a02dc278aa..c2450682510 100644 --- a/includes/wc-attribute-functions.php +++ b/includes/wc-attribute-functions.php @@ -678,6 +678,16 @@ function wc_delete_attribute( $id ) { * @return string */ function wc_attribute_taxonomy_slug( $attribute_name ) { + $cache_key = 'slug-' . $attribute_name; + $cache_value = wp_cache_get( $cache_key, 'woocommerce-attributes' ); + + if ( $cache_value ) { + return $cache_value; + } + $attribute_name = wc_sanitize_taxonomy_name( $attribute_name ); - return 0 === strpos( $attribute_name, 'pa_' ) ? substr( $attribute_name, 3 ) : $attribute_name; + $attribute_slug = 0 === strpos( $attribute_name, 'pa_' ) ? substr( $attribute_name, 3 ) : $attribute_name; + wp_cache_set( $cache_key, $attribute_slug, 'woocommerce-attributes' ); + + return $attribute_slug; } From 874f4830a6800978987505e83c9f1234b7c0a026 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 13:38:14 +0000 Subject: [PATCH 08/14] cache_value cleanup --- includes/abstracts/abstract-wc-widget.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/abstracts/abstract-wc-widget.php b/includes/abstracts/abstract-wc-widget.php index 74adb9e18f3..4ffc2e2aebb 100644 --- a/includes/abstracts/abstract-wc-widget.php +++ b/includes/abstracts/abstract-wc-widget.php @@ -289,11 +289,11 @@ abstract class WC_Widget extends WP_Widget { * @since 3.3.0 */ protected function get_current_page_url() { - $cache_key = 'woocommerce_widget_get_current_page_url'; - $cached_value = (string) wp_cache_get( $cache_key, 'woocommerce' ); + $cache_key = 'widget_current_page_url'; + $cache_value = wp_cache_get( $cache_key, 'woocommerce' ); - if ( $cached_value ) { - return apply_filters( 'woocommerce_widget_get_current_page_url', $cached_value, $this ); + if ( $cache_value ) { + return apply_filters( 'woocommerce_widget_get_current_page_url', $cache_value, $this ); } if ( defined( 'SHOP_IS_ON_FRONT' ) ) { From cb1c5792624953dc7a43c61c7aa4b4050a2de1cc Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 14:04:21 +0000 Subject: [PATCH 09/14] Cache tax class slugs --- includes/class-wc-tax.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-tax.php b/includes/class-wc-tax.php index 65be6044158..bc1c0c6fc6b 100644 --- a/includes/class-wc-tax.php +++ b/includes/class-wc-tax.php @@ -721,7 +721,16 @@ class WC_Tax { * @return array Array of class slugs ("reduced-rate", "zero-rate", etc). */ public static function get_tax_class_slugs() { - return array_filter( array_map( 'sanitize_title', self::get_tax_classes() ) ); + $cache_key = 'tax_class_slugs'; + $slugs = wp_cache_get( $cache_key, 'woocommerce' ); + + if ( ! $slugs ) { + $slugs = array_filter( array_map( 'sanitize_title', self::get_tax_classes() ) ); + + wp_cache_set( $cache_key, $slugs, 'woocommerce' ); + } + + return $slugs; } /** From 57ccde66437ade8e91d12890245d9d4c5e5e1892 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 14:13:34 +0000 Subject: [PATCH 10/14] get_product_type cache --- .../class-wc-product-data-store-cpt.php | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index c3b0b6c6b59..e4a5a78ddb7 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1503,15 +1503,27 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * @return bool|string */ public function get_product_type( $product_id ) { - $post_type = get_post_type( $product_id ); - if ( 'product_variation' === $post_type ) { - return 'variation'; - } elseif ( 'product' === $post_type ) { - $terms = get_the_terms( $product_id, 'product_type' ); - return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; - } else { - return false; + $cache_key = WC_Cache_Helper::get_cache_prefix( 'product_' . $product->get_id() ) . '_type_' . $product_id; + $product_type = wp_cache_get( $cache_key, 'products' ); + + if ( $product_type ) { + return $product_type; } + + $post_type = get_post_type( $product_id ); + + if ( 'product_variation' === $post_type ) { + $product_type = 'variation'; + } elseif ( 'product' === $post_type ) { + $terms = get_the_terms( $product_id, 'product_type' ); + $product_type = ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; + } else { + $product_type = false; + } + + wp_cache_set( $cache_key, $product_type, 'products' ); + + return $product_type; } /** From 8fc7e2be995d932cc536b7d98dfa8071aa6ba9b1 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 14:13:53 +0000 Subject: [PATCH 11/14] prefix tax cache key --- includes/class-wc-tax.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-tax.php b/includes/class-wc-tax.php index bc1c0c6fc6b..813bf9f2d55 100644 --- a/includes/class-wc-tax.php +++ b/includes/class-wc-tax.php @@ -721,13 +721,13 @@ class WC_Tax { * @return array Array of class slugs ("reduced-rate", "zero-rate", etc). */ public static function get_tax_class_slugs() { - $cache_key = 'tax_class_slugs'; - $slugs = wp_cache_get( $cache_key, 'woocommerce' ); + $cache_key = WC_Cache_Helper::get_cache_prefix( 'taxes' ) . '-slugs'; + $slugs = wp_cache_get( $cache_key, 'taxes' ); if ( ! $slugs ) { $slugs = array_filter( array_map( 'sanitize_title', self::get_tax_classes() ) ); - wp_cache_set( $cache_key, $slugs, 'woocommerce' ); + wp_cache_set( $cache_key, $slugs, 'taxes' ); } return $slugs; From 23b0fdbc1f7327b9de3463900440f02ba7a33918 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 14:57:20 +0000 Subject: [PATCH 12/14] Fix id usage --- includes/data-stores/class-wc-product-data-store-cpt.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index e4a5a78ddb7..396a5750fec 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1503,7 +1503,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * @return bool|string */ public function get_product_type( $product_id ) { - $cache_key = WC_Cache_Helper::get_cache_prefix( 'product_' . $product->get_id() ) . '_type_' . $product_id; + $cache_key = WC_Cache_Helper::get_cache_prefix( 'product_' . $product_id ) . '_type_' . $product_id; $product_type = wp_cache_get( $cache_key, 'products' ); if ( $product_type ) { From e129d18c008dfb25b815f03bd832a7bc16d124af Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 16:18:56 +0000 Subject: [PATCH 13/14] get_current_page_url should not persist --- includes/abstracts/abstract-wc-widget.php | 9 --------- includes/widgets/class-wc-widget-rating-filter.php | 3 ++- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/includes/abstracts/abstract-wc-widget.php b/includes/abstracts/abstract-wc-widget.php index 4ffc2e2aebb..eb91c48d081 100644 --- a/includes/abstracts/abstract-wc-widget.php +++ b/includes/abstracts/abstract-wc-widget.php @@ -289,13 +289,6 @@ abstract class WC_Widget extends WP_Widget { * @since 3.3.0 */ protected function get_current_page_url() { - $cache_key = 'widget_current_page_url'; - $cache_value = wp_cache_get( $cache_key, 'woocommerce' ); - - if ( $cache_value ) { - return apply_filters( 'woocommerce_widget_get_current_page_url', $cache_value, $this ); - } - if ( defined( 'SHOP_IS_ON_FRONT' ) ) { $link = home_url(); } elseif ( is_shop() ) { @@ -354,8 +347,6 @@ abstract class WC_Widget extends WP_Widget { } } - wp_cache_set( $cache_key, $link, 'woocommerce' ); - return apply_filters( 'woocommerce_widget_get_current_page_url', $link, $this ); } diff --git a/includes/widgets/class-wc-widget-rating-filter.php b/includes/widgets/class-wc-widget-rating-filter.php index 3a64f15e404..ea5580de554 100644 --- a/includes/widgets/class-wc-widget-rating-filter.php +++ b/includes/widgets/class-wc-widget-rating-filter.php @@ -99,6 +99,7 @@ class WC_Widget_Rating_Filter extends WC_Widget { $found = false; $rating_filter = isset( $_GET['rating_filter'] ) ? array_filter( array_map( 'absint', explode( ',', wp_unslash( $_GET['rating_filter'] ) ) ) ) : array(); // WPCS: input var ok, CSRF ok, sanitization ok. + $base_link = $this->get_current_page_url(); $this->widget_start( $args, $instance ); @@ -110,7 +111,7 @@ class WC_Widget_Rating_Filter extends WC_Widget { continue; } $found = true; - $link = $this->get_current_page_url(); + $link = $base_link; if ( in_array( $rating, $rating_filter, true ) ) { $link_ratings = implode( ',', array_diff( $rating_filter, array( $rating ) ) ); From 5b57cb535653bcba8c7ae901c6fb70580a4a7279 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 30 Jan 2019 16:29:01 +0000 Subject: [PATCH 14/14] Layered nav should use base link --- includes/widgets/class-wc-widget-layered-nav.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/widgets/class-wc-widget-layered-nav.php b/includes/widgets/class-wc-widget-layered-nav.php index b51d1871fc7..be045fea349 100644 --- a/includes/widgets/class-wc-widget-layered-nav.php +++ b/includes/widgets/class-wc-widget-layered-nav.php @@ -405,6 +405,7 @@ class WC_Widget_Layered_Nav extends WC_Widget { $term_counts = $this->get_filtered_term_product_counts( wp_list_pluck( $terms, 'term_id' ), $taxonomy, $query_type ); $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes(); $found = false; + $base_link = $this->get_current_page_url(); foreach ( $terms as $term ) { $current_values = isset( $_chosen_attributes[ $taxonomy ]['terms'] ) ? $_chosen_attributes[ $taxonomy ]['terms'] : array(); @@ -431,7 +432,7 @@ class WC_Widget_Layered_Nav extends WC_Widget { $current_filter[] = $term->slug; } - $link = remove_query_arg( $filter_name, $this->get_current_page_url() ); + $link = remove_query_arg( $filter_name, $base_link ); // Add current filters to URL. foreach ( $current_filter as $key => $value ) {