From dfbe3e2e16f0fb1afc9d05d9a4dcf09efd0b1ce1 Mon Sep 17 00:00:00 2001 From: claudiosmweb Date: Mon, 2 Jun 2014 17:00:44 -0300 Subject: [PATCH] excluded the product_cat custom rewrites from the rewrite_rules_array, closes the #4844 --- includes/wc-core-functions.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index bc0d8fe3923..520a0dbd7b8 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -408,3 +408,27 @@ function get_woocommerce_api_url( $path ) { function wc_get_log_file_path( $handle ) { return trailingslashit( WC_LOG_DIR ) . $handle . '-' . sanitize_file_name( wp_hash( $handle ) ) . '.log'; } + +/** + * Fix the rewrite rules when the product permalink have %product_cat% flag. + * + * @since 2.2 + * @param array $rules + * @return array + */ +function wc_fix_rewrite_rules( $rules ) { + $permalinks = get_option( 'woocommerce_permalinks' ); + $product_permalink = empty( $permalinks['product_base'] ) ? _x( 'product', 'slug', 'woocommerce' ) : $permalinks['product_base']; + + if ( preg_match( '/\/(.+)(\/%product_cat%)/' , $product_permalink, $matches ) ) { + foreach ( $rules as $rule => $rewrite ) { + if ( preg_match( '/^' . $matches[1] . '/', $rule ) && preg_match( '/^(index\.php\?product_cat)(?!(.*product))/', $rewrite ) ) { + unset( $rules[ $rule ] ); + } + } + } + + return $rules; +} + +add_filter( 'rewrite_rules_array', 'wc_fix_rewrite_rules' );