• Resolved edosantillana

    (@edosantillana)


    Hello,
    I’m creating a multisite for a shop in two languages: Spanish (main) and English (subdomain en.).
    I’m working now in the English version created with MultilingualPress.
    The problem is when I change from a product in English like:
    en.domain.com/shop/category-product/product to the Spanish version it shows:
    domain.com/shop/category-product/product and this is a 404 because it should be:
    domain.com/tienda/category-product/product the permalink structure do not shows the spanish version of the permalink shop base. By the way, I’m using WooCommerce as ecommerce plugin.
    I tried using the dynamics permalinks suggested by the plugin and it works, but it shows a not SEO friendly URL like domain.com/?product=something
    There is a way to make it work without sacrifice the original permalink structure?
    Thanks in advance.
    Regards.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support dinamiko

    (@dinamiko)

    Hi edosantillana,

    You need to filter and replace custom post type slugs via mlp_linked_element_link filter so for example if your site IDs are Spanish 1 and English 2, the code could be like so:

    function filter_mlp_linked_cpt_archive_link(
    	$url,
    	$target_site_id,
    	$target_content_id,
    	Mlp_Translation_Interface $translation
    ) {
    
    	if ( ! is_singular( 'product' ) ) {
    		return $url;
    	}
    
    	if ( ! is_a( $translation, 'Mlp_Translation_Interface' ) ) {
    		return $url;
    	}
    
    	$source_site_id = $translation->get_source_site_id();
    	if ( ! $source_site_id ) {
    		return $url;
    	}
    
    	if( is_singular( 'product' ) ) {
    
    		$fragments = array(
    			1 => array(
    				'/tienda/',
    			),
    			2 => array(
    				'/shop/',
    			)
    		);
    	}
    
    	if ( empty( $fragments[ $source_site_id ] ) ) {
    		return $url;
    	}
    
    	if ( empty( $fragments[ $target_site_id ] ) ) {
    		return $url;
    	}
    
    	return str_replace(
    		$fragments[ $source_site_id ],
    		$fragments[ $target_site_id ],
    		$url
    	);
    }
    
    add_filter( 'mlp_linked_element_link', 'filter_mlp_linked_cpt_archive_link', 10, 4 );

    Notice $fragments array where we add key value pairs, keys are site IDs and values are arrays with custom post type slugs.

    The above example only implements single product page, it uses is_singular( ‘product’ ), you can of course add more logic inside to implement links in other pages like product categories (is_post_type_archive, is_archive…) and so on.

    • This reply was modified 7 years ago by dinamiko.
    Thread Starter edosantillana

    (@edosantillana)

    Hello dinamiko,

    Thanks for your answer, it works great on single products but I couldn’t make it work for product categories and shop page itself.

    This is what I wrote in the code to try make it work for product categories:

    function filter_mlp_linked_cpt_archive_link(
    	$url,
    	$target_site_id,
    	$target_content_id,
    	Mlp_Translation_Interface $translation
    ) {
    	if ( ! is_singular( 'product' ) ) {
    		return $url;
    	}
    	if ( ! is_a( $translation, 'Mlp_Translation_Interface' ) ) {
    		return $url;
    	}
    	$source_site_id = $translation->get_source_site_id();
    	if ( ! $source_site_id ) {
    		return $url;
    	}
    	if( is_singular( 'product' ) ) {
    		$fragments = array(
    			1 => array(
    				'/tienda/',
    			),
    			2 => array(
    				'/shop/',
    			)
    		);
    	}
    	if( is_post_type_archive( 'product_cat' ) ) {
    		$fragments = array(
    			1 => array(
    				'/categoria-producto/',
    			),
    			2 => array(
    				'/product-category/',
    			)
    		);
    	}
    	if ( empty( $fragments[ $source_site_id ] ) ) {
    		return $url;
    	}
    	if ( empty( $fragments[ $target_site_id ] ) ) {
    		return $url;
    	}
    	return str_replace(
    		$fragments[ $source_site_id ],
    		$fragments[ $target_site_id ],
    		$url
    	);
    }
    
    add_filter( 'mlp_linked_element_link', 'filter_mlp_linked_cpt_archive_link', 10, 4 );

    I’m kind of newbie coding. What am I doing wrong? No luck with the shop page neither.

    Thanks in advance.

    Plugin Support dinamiko

    (@dinamiko)

    Hi edosantillana,

    Try this one, notice the addition of is_post_type_archive (for shop page) and is_archive (for product category pages):

    function filter_mlp_linked_cpt_archive_link(
    	$url,
    	$target_site_id,
    	$target_content_id,
    	Mlp_Translation_Interface $translation
    ) {
    
    	if ( ! is_singular( 'product' ) && ! is_post_type_archive() && ! is_archive() ) {
    		return $url;
    	}
    
    	if ( ! is_a( $translation, 'Mlp_Translation_Interface' ) ) {
    		return $url;
    	}
    
    	$source_site_id = $translation->get_source_site_id();
    	if ( ! $source_site_id ) {
    		return $url;
    	}
    
    	if( is_singular( 'product' ) || is_post_type_archive() ) {
    
    		$fragments = array(
    			1 => array(
    				'/tienda/',
    			),
    			2 => array(
    				'/shop/',
    			)
    		);
    	}
    	else {
    
    		$fragments = array(
    			1 => array(
    				'/categoria-producto/',
    			),
    			2 => array(
    				'/product-category/',
    			)
    		);
    	}
    
    	if ( empty( $fragments[ $source_site_id ] ) ) {
    		return $url;
    	}
    
    	if ( empty( $fragments[ $target_site_id ] ) ) {
    		return $url;
    	}
    
    	return str_replace(
    		$fragments[ $source_site_id ],
    		$fragments[ $target_site_id ],
    		$url
    	);
    }
    
    add_filter( 'mlp_linked_element_link', 'filter_mlp_linked_cpt_archive_link', 10, 4 );
    Thread Starter edosantillana

    (@edosantillana)

    Great! That’s make the trick.
    Thank you dinamiko.
    Just one last thing (or thought).
    This problem should be solve by default from the plugin, don’t you think? I mean, something so important like correct permalinks translations is a primary function expected.
    Is the first time that I use this plugin, I used to work with WPML, but I’m happy with the result and for sure I will keep using it from now, but this is a strange issue not expected. I suppose is hard to implement this solution by default.
    Anyway is a great plugin and I’m sure you and your team will keep improving it.
    Thanks again dinamiko.
    Best regards.

    pedropablopicamontesinos

    (@pedropablopicamontesinos)

    Hello both.

    My settings are these:

    english site (main site):
    Category product slug: domain.com/custom-word1/category-product-slug
    Tag product slug: domain.com/custom-word2/tag-category-slug
    Shop: domain.com/shop
    Product: domain.com/custom-word3/product-slug

    spanish site:
    Category product slug: domain.com/es/custom-palabra1/category-product-slug
    Tag product slug: domain.com/es/custom-word2/tag-category-slug
    Shop: domain.com/es/tienda
    Product: domain.com/es/custom-palabra3/product-slug

    Your code offer an awesome solution for “category product” and for “shop page” with different slug for every site.

    I have same base slug (custom-word2) for “tags product slug”… few words are the same in english and in spanish. I found one! So, I don’t need tweak the code for base “tag product slug”.

    But… I would like a solution for simple products with different slug base.

    In my case, if I use “dynamic permalinks” for products, the language switcher works fine. Buy a pretty-permalink will be always pretty (for my eyes and for Google eyes).

    The domain.com/?product=product-slug is a functional solution but it is not a nice solution.

    I prefer to change “custom-word3” to “custom-palabra3”:

    domain.com/custom-word3/product-slug —> domain.com/es/custom-palabra3/product-slug

    I tried make the code… and it is work. But… I prefer the developer author @dinamiko supervises it. @edosantillana, if you try the new code in your site and it is work fine, please post a reply.

    
    
    <?php
    
    // -----------------------------------------------------------------------------
    // Filter for Change the Base Permalink for Shop and Products Category - Multilingual Press
    // -----------------------------------------------------------------------------
    
    function filter_mlp_linked_cpt_archive_link(
    	$url,
    	$target_site_id,
    	$target_content_id,
    	Mlp_Translation_Interface $translation
    ) {
    
    	if ( ! is_singular( 'product' ) && ! is_post_type_archive() && ! is_archive() ) {
    		return $url;
    	}
    
    	if ( ! is_a( $translation, 'Mlp_Translation_Interface' ) ) {
    		return $url;
    	}
    
    	$source_site_id = $translation->get_source_site_id();
    	if ( ! $source_site_id ) {
    		return $url;
    	}
    
    	if( is_singular( 'product' ) || is_post_type_archive() ) {
    
    		$fragments = array(
    			1 => array(
    				'/shop/',
    			),
    			2 => array(
    				'/tienda/',
    			)
    		);
    	}
    	else {
    
    		$fragments = array(
    			1 => array(
    				'/custom-word1/',
    			),
    			2 => array(
    				'/custom-palabra1/',
    			)
    		);
    	}
        if( is_singular( 'product' ) ) {
    
    	   $fragments = array(
    		  1 => array(
    			     '/custom-word3/',
    		  ),
    		  2 => array(
    			     '/custom-palabra3/',
    		  )
    	   );
        }
    	if ( empty( $fragments[ $source_site_id ] ) ) {
    		return $url;
    	}
    
    	if ( empty( $fragments[ $target_site_id ] ) ) {
    		return $url;
    	}
    
    	return str_replace(
    		$fragments[ $source_site_id ],
    		$fragments[ $target_site_id ],
    		$url
    	);
    }
    
    add_filter( 'mlp_linked_element_link', 'filter_mlp_linked_cpt_archive_link', 10, 4 );
    
    

    My addition is:

    
    
    if( is_singular( 'product' ) ) {
    
    	   $fragments = array(
    		  1 => array(
    			     '/custom-word3/',
    		  ),
    		  2 => array(
    			     '/custom-palabra3/',
    		  )
    	   );
        }
    
    

    Really thanks thanks and thanks for your support.

    Posdata: +1 for “base slug” support in the core and default settings options of the plugin. I used and suffer WPML for years and MultilingualPress is the final solution for multilingual WP sites. But this feature is very important.

    Thread Starter edosantillana

    (@edosantillana)

    Hello @pedropablopicamontesinos,

    In my case I just need the solution @dinamiko provides me, I tried it and works great.
    Thank you for posting the solution for your case, I’m sure that someone will use it in the future.
    I agree with you about the importance of this feature, MultilingualPress is a really nice option to WPML and I hope it will still being it.

    Good luck.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Problems with shop permalinks’ is closed to new replies.