Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by Ton 2 months, 3 weeks ago.

Author Posts
August 26, 2024 at 10:16 am #16103818

Ton

Background of the issue:
I am trying to resolve an issue with a translated product category showing a 404 not found error. Please read this ticket: https://wpml.org/forums/topic/translated-product-category-shows-404-not-found/. I have another website where this setup does work: hidden link

Symptoms:
The translated product category shows a 404 not found error. The solution provided in the ticket is not a solution.

Questions:
No questions, i expect a solution where this gets fixed.

August 27, 2024 at 5:43 am #16107343

Ton

For info: I believe when the back-end URL's are the right ones, we set up in the wpml, than all should be good.

After that I use the following code to make things work on the front-end. This is how we did it with watervision. So the problem is the MISMATCH in the settings and what you actually see in the back-end when viewing the overview of product categories. There is a wrong slug for the languages.

function devvn_product_category_base_same_shop_base( $flash = false ){
if(defined('ICL_LANGUAGE_CODE')){
global $sitepress;
$languages = icl_get_languages('skip_missing=0&orderby=code');
if($languages && !empty($languages)){
$original_lang = ICL_LANGUAGE_CODE;
foreach($languages as $key=>$lang) {
$new_lang = $key;
$sitepress->switch_lang($new_lang);
$terms = get_terms(array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'hide_empty' => false,
));
if ($terms && !is_wp_error($terms)) {
$siteurl = apply_filters( 'wpml_home_url', get_home_url('/'));
$siteurl = ($sitepress->get_default_language() == $key) ? $siteurl.'/' : $siteurl;
foreach ($terms as $term) {
$term_slug = $term->slug;
$baseterm = str_replace($siteurl, '', get_term_link($term->term_id, 'product_cat'));
add_rewrite_rule($baseterm . '?$', 'index.php?product_cat=' . $term_slug, 'top');
add_rewrite_rule($baseterm . 'page/([0-9]{1,})/?$', 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]', 'top');
add_rewrite_rule($baseterm . '(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat=' . $term_slug . '&feed=$matches[1]', 'top');
}
}
$sitepress->switch_lang($original_lang);
}
}
}
else{
$terms = get_terms(array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'hide_empty' => false,
));
if ($terms && !is_wp_error($terms)) {
$siteurl = esc_url(home_url('/'));
foreach ($terms as $term) {
$term_slug = $term->slug;
$baseterm = str_replace($siteurl, '', get_term_link($term->term_id, 'product_cat'));

add_rewrite_rule($baseterm . '?$','index.php?product_cat=' . $term_slug,'top');
add_rewrite_rule($baseterm . 'page/([0-9]{1,})/?$', 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]','top');
add_rewrite_rule($baseterm . '(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat=' . $term_slug . '&feed=$matches[1]','top');

}
}
}
if ($flash == true){
flush_rewrite_rules(false);
}
}
add_filter( 'init', 'devvn_product_category_base_same_shop_base');

//Fix error when creating new taxomony 404
add_action( 'create_term', 'devvn_product_cat_same_shop_edit_success', 10, 2 );
function devvn_product_cat_same_shop_edit_success( $term_id, $taxonomy ) {
devvn_product_category_base_same_shop_base(true);
}

August 27, 2024 at 11:24 am #16109183

Ton

Not trusting you guys will come up with a working solution, altough i have other sites that have this setup too, I hereby close this ticket.

I will fix it myself.