URL товара и категории с одинаковым префиксом Woocommerce
Есть код, который в Woocommerce меняет базу в url категорий на базу url магазина. То есть если у нас база url магазина products
, то нужно в настройках Постоянных ссылок выставить Базу категорий товара тоже products
а для постоянных ссылок товара выбрать такое /products/%product_cat%/
. Потом вставляем нижеследующий код в functions.php и все работает.
/**
* Set product category base url same as a shop base
*
* @param array $rules
*
* @see https://gist.github.com/levantoan/fc705c5ae4739e6d87e2ec51b257ea5c
* @return array
*/
add_filter( 'rewrite_rules_array',
function( $rules ) {
$new_rules = array();
global $sitepress;
if ( $sitepress ) {
// Disable wpml filters to get all the product terms.
remove_filter( 'get_terms_args', array( $sitepress, 'get_terms_args_filter' ) );
remove_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ) );
remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
// Get the terms.
$terms = get_terms( array( 'taxonomy' => 'product_cat', 'post_type' => 'product', 'hide_empty' => false ) );
} else {
$terms = get_terms( array( 'taxonomy' => 'product_cat', 'post_type' => 'product', 'hide_empty' => false ) );
}
// Check if any terms are present and the there are no errors.
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
// Detect term language.
$language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => (int) $term->term_taxonomy_id, 'element_type' => 'product_cat' ) );
// Check the language code.
if ( $language_code === 'en' ) {
$siteurl = esc_url( home_url( '/en/' ) );
} else {
$siteurl = esc_url( home_url( '/' ) );
}
$term_slug = $term->slug;
$baseterm = str_replace( $siteurl, '', get_term_link( $term->term_id, 'product_cat' ) );
// Rules for a specific category.
$new_rules[ $baseterm . '?$' ] = 'index.php?product_cat=' . $term_slug;
// Rules for a category pagination.
$new_rules[ $baseterm . '/page/([0-9]{1,})/?$' ] = 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]';
$new_rules[ $baseterm . '(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?product_cat=' . $term_slug . '&feed=$matches[1]';
}
}
if ( $sitepress ) {
add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10, 4 );
add_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1, 1 );
add_filter( 'get_terms_args', array( $sitepress, 'get_terms_args_filter' ), 10, 2 );
}
return $new_rules + $rules;
}
);
/**
* Flush rewrite rules when create new term
* need for a new product category rewrite rules
*
*/
function imp_create_term() {
flush_rewrite_rules( false );
}
add_action( 'create_term', 'imp_create_term' );
Код работает нормально. Но Мне нужно поменять изменить структуру url для товара, исключив в нем категорию. То есть сейчас url товара выглядит так - site.com/products/category-name/product-name
, а нужно чтобы было site.com/products/product-name
. Понятно, что нужно изменить постоянную ссылку на товар в настройках Постоянных ссылок, но тогда на месте страницы товара появляется ошибка 404. Подскажите как изменить этот код, чтобы все работало нормально???