// SLUG FUNCTION with Polylang add_filter( 'term_link', 'remove_product_category_base_slug', 10, 3 ); function remove_product_category_base_slug( $url, $term, $taxonomy ) { if ( $taxonomy !== 'product_cat' ) return $url; $lang = function_exists('pll_get_term_language') ? pll_get_term_language( $term->term_id ) : ''; $lang_prefix = $lang && $lang !== pll_default_language() ? '/' . $lang : ''; return home_url( $lang_prefix . '/' . get_term_full_slug( $term ) . '/' ); } add_action( 'init', 'custom_product_cat_rewrite_rules', 10 ); function custom_product_cat_rewrite_rules() { $languages = function_exists('pll_languages_list') ? pll_languages_list() : [ get_locale() ]; foreach ( $languages as $lang ) { $terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'lang' => $lang, ) ); if ( is_wp_error( $terms ) ) continue; foreach ( $terms as $term ) { $slug = get_term_full_slug( $term ); $lang_prefix = $lang !== pll_default_language() ? $lang . '/' : ''; add_rewrite_rule( '^' . $lang_prefix . $slug . '/?$', 'index.php?product_cat=' . $term->slug . '&lang=' . $lang, 'top' ); } } } add_filter( 'post_type_link', 'custom_full_product_permalink', 10, 2 ); function custom_full_product_permalink( $permalink, $post ) { if ( $post->post_type !== 'product' ) return $permalink; $terms = get_the_terms( $post->ID, 'product_cat' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $term = get_lowest_level_term( $terms ); $slug = get_term_full_slug( $term ); $lang = function_exists('pll_get_post_language') ? pll_get_post_language( $post->ID ) : ''; $lang_prefix = $lang && $lang !== pll_default_language() ? '/' . $lang : ''; return home_url( $lang_prefix . '/' . $slug . '/' . $post->post_name . '/' ); } return home_url( '/' . $post->post_name . '/' ); } add_action( 'init', 'custom_full_product_rewrite_rules' ); function custom_full_product_rewrite_rules() { $languages = function_exists('pll_languages_list') ? pll_languages_list() : [ get_locale() ]; foreach ( $languages as $lang ) { $products = get_posts([ 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => 'publish', 'lang' => $lang, ]); foreach ( $products as $product ) { $terms = get_the_terms( $product->ID, 'product_cat' ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $term = get_lowest_level_term( $terms ); $slug = get_term_full_slug( $term ); $lang_prefix = $lang !== pll_default_language() ? $lang . '/' : ''; $rule = '^' . $lang_prefix . $slug . '/' . $product->post_name . '/?$'; $query = 'index.php?post_type=product&name=' . $product->post_name . '&lang=' . $lang; add_rewrite_rule( $rule, $query, 'top' ); } } } } function get_lowest_level_term( $terms ) { usort( $terms, function( $a, $b ) { return count( get_ancestors( $b->term_id, 'product_cat' ) ) <=> count( get_ancestors( $a->term_id, 'product_cat' ) ); } ); return $terms[0]; } function get_term_full_slug( $term ) { $slugs = []; $ancestors = get_ancestors( $term->term_id, 'product_cat' ); $ancestors = array_reverse( $ancestors ); foreach ( $ancestors as $ancestor_id ) { $ancestor = get_term( $ancestor_id, 'product_cat' ); $slugs[] = $ancestor->slug; } $slugs[] = $term->slug; return implode( '/', $slugs ); } add_action('init', function() { flush_rewrite_rules(); });