Commit 96a33c64646 for woocommerce
commit 96a33c64646d71aec1834019754d8954d66ff034
Author: Ján Mikláš <neosinner@gmail.com>
Date: Fri Nov 27 16:04:58 2026 +0100
Warn about conflicting product category permalinks (#71380)
* Warn about conflicting product category permalinks
* Add changelog entry for permalink warning
diff --git a/plugins/woocommerce/changelog/fix-product-category-permalink-conflict b/plugins/woocommerce/changelog/fix-product-category-permalink-conflict
new file mode 100644
index 00000000000..3f85805ca8e
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-product-category-permalink-conflict
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Warn when shared product and category permalink bases can make a category archive unavailable.
diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-permalink-settings.php b/plugins/woocommerce/includes/admin/class-wc-admin-permalink-settings.php
index c8aa4f3b419..c63e2210751 100644
--- a/plugins/woocommerce/includes/admin/class-wc-admin-permalink-settings.php
+++ b/plugins/woocommerce/includes/admin/class-wc-admin-permalink-settings.php
@@ -189,10 +189,96 @@ class WC_Admin_Permalink_Settings {
return '/' . $default_base === $base ? $default_base : $base;
}
+ /**
+ * Warn when the shared product prefix leaves a sampled category URL without an archive rule.
+ */
+ private function show_category_permalink_warning(): void {
+ global $wp_rewrite;
+
+ if (
+ ! $wp_rewrite instanceof WP_Rewrite ||
+ empty( $wp_rewrite->rules ) ||
+ false === has_filter( 'rewrite_rules_array', 'wc_fix_rewrite_rules' ) ||
+ ! preg_match( '~/(.+)/%product_cat%~', $this->permalinks['product_rewrite_slug'], $matches )
+ ) {
+ return;
+ }
+
+ // Use the registered structure so WordPress's rewrite root is already included.
+ $structure = $wp_rewrite->get_extra_permastruct( 'product_cat' );
+ $prefix = $matches[1] . '/';
+ $taxonomy = get_taxonomy( 'product_cat' );
+ if ( ! is_string( $structure ) || ltrim( $structure, '/' ) !== $prefix . '%product_cat%' || ! $taxonomy || 'product_cat' !== $taxonomy->query_var ) {
+ return;
+ }
+
+ $terms = get_terms(
+ array(
+ 'taxonomy' => 'product_cat',
+ 'parent' => 0,
+ 'hide_empty' => false,
+ 'number' => 1,
+ )
+ );
+ if ( ! is_array( $terms ) || ! isset( $terms[0] ) || ! $terms[0] instanceof WP_Term ) {
+ return;
+ }
+
+ $url = get_term_link( $terms[0] );
+ if ( ! is_string( $url ) ) {
+ return;
+ }
+
+ $archive = wp_parse_url( $url );
+ $home = wp_parse_url( home_url( '/' ) );
+ if ( empty( $archive['path'] ) || empty( $archive['host'] ) || ( $home['host'] ?? '' ) !== $archive['host'] || isset( $archive['query'] ) ) {
+ return;
+ }
+
+ $home_path = trailingslashit( $home['path'] ?? '' );
+ if ( 0 !== strpos( $archive['path'], $home_path ) ) {
+ return;
+ }
+
+ $path = trim( substr( $archive['path'], strlen( $home_path ) ), '/' );
+ $decoded_path = urldecode( $path );
+ if ( ! str_starts_with( $path, $prefix ) && ! str_starts_with( $decoded_path, $prefix ) ) {
+ return;
+ }
+
+ // Check the final rules so replacements added by other plugins count.
+ foreach ( $wp_rewrite->rules as $pattern => $target ) {
+ if ( ! str_contains( $target, 'product_cat' ) || ( ! preg_match( "#^$pattern#", $path ) && ! preg_match( "#^$pattern#", $decoded_path ) ) ) {
+ continue;
+ }
+
+ parse_str( (string) wp_parse_url( $target, PHP_URL_QUERY ), $query );
+ $is_category = isset( $query['product_cat'] ) || ( 'product_cat' === ( $query['taxonomy'] ?? '' ) && isset( $query['term'] ) );
+ if ( $is_category && ! isset( $query['product'] ) ) {
+ return;
+ }
+ }
+
+ wp_admin_notice(
+ sprintf(
+ /* translators: %s: A link to the product category archive. */
+ esc_html__( 'The product category archive at %s may be unavailable. Use different bases for product categories and product permalinks.', 'woocommerce' ),
+ '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>'
+ ),
+ array(
+ 'type' => 'warning',
+ 'id' => 'woocommerce-product-category-permalink-warning',
+ 'additional_classes' => array( 'inline' ),
+ )
+ );
+ }
+
/**
* Show the settings.
*/
public function settings() {
+ $this->show_category_permalink_warning();
+
/* translators: %s: Home URL */
echo wp_kses_post( wpautop( sprintf( __( 'If you like, you may enter custom structures for your product URLs here. For example, using <code>shop</code> would make your product links like <code>%sshop/sample-product/</code>. This setting affects product URLs only, not things such as product categories.', 'woocommerce' ), esc_url( home_url( '/' ) ) ) ) );
diff --git a/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-permalink-settings-test.php b/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-permalink-settings-test.php
index 919b08c2616..f6cf04d3a21 100644
--- a/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-permalink-settings-test.php
+++ b/plugins/woocommerce/tests/php/includes/admin/class-wc-admin-permalink-settings-test.php
@@ -149,6 +149,69 @@ class WC_Admin_Permalink_Settings_Test extends WC_Unit_Test_Case {
return (string) $this->capture_output_from( array( $sut, 'settings' ) );
}
+ /**
+ * @testdox Should warn only when the shared permalink prefix leaves the sampled category URL without an archive rule.
+ * @testWith ["/%postname%/", "collections", "/collections/%product_cat%", true]
+ * ["/%postname%/", "collections", "catalog/collections/%product_cat%", true]
+ * ["/%postname%/", "product-category", "/collections/%product_cat%", false]
+ * ["/index.php/%postname%/", "collections", "catalog/collections/%product_cat%", false]
+ * ["/%postname%/", "collections", "/collections/%product_cat%", false, "index.php?product_cat=$matches[1]"]
+ * ["/%postname%/", "collections", "/collections/%product_cat%", false, "index.php?taxonomy=product_cat&term=$matches[1]"]
+ * ["/%postname%/", "collections", "/collections/%product_cat%", false, null, "category-archive"]
+ * ["", "collections", "/collections/%product_cat%", false]
+ * @param string $structure WordPress permalink structure.
+ * @param string $category_base WooCommerce category base.
+ * @param string $product_base WooCommerce product base.
+ * @param bool $expect_warning Whether the settings show the warning.
+ * @param string|null $alternate_query Rewrite target for an extension's replacement archive rules.
+ * @param string|null $link_base Base used when an extension supplies alternate category URLs.
+ */
+ public function test_category_permalink_warning_reads_generated_rules( string $structure, string $category_base, string $product_base, bool $expect_warning, ?string $alternate_query = null, ?string $link_base = null ): void {
+ global $wp_rewrite;
+
+ self::factory()->term->create( array( 'taxonomy' => 'product_cat' ) );
+ update_option( 'home', home_url( '/store' ) );
+ update_option( 'permalink_structure', $structure );
+ update_option(
+ 'woocommerce_permalinks',
+ array(
+ 'category_base' => $category_base,
+ 'product_base' => $product_base,
+ )
+ );
+
+ if ( null !== $link_base ) {
+ add_filter(
+ 'term_link',
+ static fn ( $link, $term, $taxonomy ) => 'product_cat' === $taxonomy ? home_url( '/' . $link_base . '/' . $term->slug . '/' ) : $link,
+ 10,
+ 3
+ );
+ }
+
+ if ( null !== $alternate_query ) {
+ add_filter(
+ 'rewrite_rules_array',
+ static fn ( $rules ) => array( $category_base . '/([^/]+)/?$' => $alternate_query ) + $rules,
+ 99
+ );
+ }
+
+ $original_rewrite = $wp_rewrite;
+ $wp_rewrite = new WP_Rewrite(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Isolate the generated rules for this test.
+ try {
+ $wp_rewrite->add_rewrite_tag( '%product_cat%', '(.+?)', 'product_cat=' );
+ $wp_rewrite->add_rewrite_tag( '%product%', '([^/]+)', 'product=' );
+ $wp_rewrite->add_permastruct( 'product_cat', $category_base . '/%product_cat%', array( 'with_front' => false ) );
+ $wp_rewrite->add_permastruct( 'product', $product_base . '/%product%', array( 'with_front' => false ) );
+ $wp_rewrite->rewrite_rules();
+
+ $this->assertSame( $expect_warning, str_contains( $this->render_settings(), 'id="woocommerce-product-category-permalink-warning"' ) );
+ } finally {
+ $wp_rewrite = $original_rewrite; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restore the original rewrite object.
+ }
+ }
+
/**
* Parse rendered settings HTML into an XPath query object.
*