Support » Plugin: MultilingualPress » Bad redirect in WooCommerce Shop Page

  • I have a multisite configured by subdomain, with a domain mapped in a second site.

    I’ve created a function in a plugin and all it’s ok. But, when I click in a language link of Woocommerce shop page, the link is incorrect; the link change the domain (ok) but maintain the original slug, not the slug of the another language.

    /The other pages redirect ok/

    The function of the plugin:

    
    function agm_add_mlp4menu ($items, $args) {
    	if ( class_exists('Mlp_Helpers') ) {
    	    if ( $args->theme_location == 'primary' ){
    	        
    	        $output = '';
    	        $output .= Mlp_Helpers::show_linked_elements( array( 'link_text' => 'language_short', 'show_current_blog' => FALSE, 'display_flag' => FALSE ) );
    	        $output = str_replace( '<div class="mlp-language-box mlp_language_box"><ul>', '', $output );
    	        $output = str_replace( '<li>', '<li class="menu-item-language">', $output );
    	        $output = str_replace( '</ul></div>', '', $output );
    	        
    	        $items .= $output;
    	    }
    	    return $items;
    	}
    }
    
    add_filter('wp_nav_menu_items', 'agm_add_mlp4menu', 10, 2);
    

    EXAMPLES

    — When I click the link “EN” to change from spanish to english, The link go to

    — When I click the link “ES” to change from english to spanish, The link go to

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author dinamiko

    (@dinamiko)

    Hi Alejandro,

    You can adapt MultilingualPress (MLP) dinamically generated language items URLs by using mlp_linked_element_link filter, you need to adapt $fragments array key value pairs based on your site IDs and slugs, here is a WooCommerce example that you can use as starting point:

    https://wordpress.org/support/topic/problems-with-shop-permalinks/#post-9714350

    The above approach only works for language links generated by MLP, if you want to create language items manually then you can use this example as stating point, it creates a language select and adds it to the sidebar:

    // create select
    function mlp_custom_get_dropdown_menu() {
    
    	$items = mlp_custom_get_language_items();
    	?>
    	<select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
    		<?php var_dump($items);?>
    		<?php foreach ( $items as $item ) { ?>
    			<option <?php echo $item['active'] == true ? 'selected="selected"' : '';?> value="<?php echo esc_url($item['url']);?>"><?php echo $item['name'];?></option>
    		<?php } ?>
    	</select>
    	<?php
    }
    
    /**
     * Get language items.
     *
     * @return array|void
     */
    function mlp_custom_get_language_items() {
    
    	$api = apply_filters( 'mlp_language_api', NULL );
    	if ( ! is_a( $api, 'Mlp_Language_Api_Interface' ) ) {
    		return;
    	}
    
    	/**
    	 * @type int    $site_id      Base site
    	 * @type int    $content_id   post or term_taxonomy ID, *not* term ID
    	 * @type string $type         @see Mlp_Language_Api::get_request_type()
    	 * @type bool   $strict       When TRUE (default) only matching exact
    	 *                                 translations will be included
    	 * @type string $search_term  If you want to translate a search
    	 * @type string $post_type    For post type archives
    	 * @type bool   $include_base Include the base site in returned list
    	 */
    	$translations_args = array(
    		'strict'       => FALSE,
    		'include_base' => TRUE,
    	);
    	$translations      = $api->get_translations( $translations_args );
    	if ( empty( $translations ) ) {
    		return;
    	}
    
    	$items = array();
    	/** @var Mlp_Translation_Interface $translation */
    	foreach ( $translations as $site_id => $translation ) {
    
    		$url = $translation->get_remote_url();
    		if ( empty( $url ) ) {
    			continue;
    		}
    
    		$language = $translation->get_language();
    		$active   = FALSE;
    		if ( get_current_blog_id() === $site_id ) {
    			$active = TRUE;
    		}
    
    		$items[ $site_id ] = array(
    			'url'    => $url,
    			'http'   => $language->get_name( 'http' ),
    			'name'   => $language->get_name( 'native' ),
    			'active' => $active,
    		);
    	}
    
    	return $items;
    } 
    
    // Add select to sidebar
    add_action( 'mlp_and_wp_loaded', function() {
    	add_action( 'get_sidebar', 'mlp_custom_get_dropdown_menu' );
    });

    Thanks,
    Emili

    Thanks @dinamiko

    I did it! I have already added to the menu the links to change the language; furthermore, the link of the WooCommerce store is correct now.

    
    /*
     * Add links to the menus
     **/
    
    function agm_add_links_menu_mlp4sf ($items, $args) {
    
    	$langs = mlp_custom_get_language_items();
    
        if ( $args->theme_location == 'primary' || $args->theme_location == 'handheld' )
        {
            foreach ( $langs as $site_id => $lang )
            {
    			if ( ! $lang['active'] ) $items .= '<li class="menu-item-language lang-' . $site_id . '"><a rel="alternate" hreflang="' . $lang['code'] . '" href="' . $lang['url'] . '">' . $lang['name'] . '</a></li>';
    		}
        }
    
        return $items;
    }
    
    /**
     * Get language items.
     *
     * @return array|void
     */
    function mlp_custom_get_language_items() {
    
    	$api = apply_filters( 'mlp_language_api', NULL );
    	if ( ! is_a( $api, 'Mlp_Language_Api_Interface' ) ) {
    		return;
    	}
    
    	/**
    	 * @type int    $site_id      Base site
    	 * @type int    $content_id   post or term_taxonomy ID, *not* term ID
    	 * @type string $type         @see Mlp_Language_Api::get_request_type()
    	 * @type bool   $strict       When TRUE (default) only matching exact
    	 *                                 translations will be included
    	 * @type string $search_term  If you want to translate a search
    	 * @type string $post_type    For post type archives
    	 * @type bool   $include_base Include the base site in returned list
    	 */
    	$translations_args = array(
    		'strict'       => FALSE,
    		'include_base' => TRUE,
    	);
    	$translations      = $api->get_translations( $translations_args );
    	if ( empty( $translations ) ) {
    		return;
    	}
    
    	$items = array();
    	/** @var Mlp_Translation_Interface $translation */
    	foreach ( $translations as $site_id => $translation ) {
    
    		if ( is_shop() ) $url = get_blog_permalink( $site_id, get_blog_option( $site_id, "woocommerce_shop_page_id", 0 ) );
    		else $url = $translation->get_remote_url();
    		
    		if ( empty( $url ) ) {
    			continue;
    		}
    
    		$language = $translation->get_language();
    		$active   = FALSE;
    		if ( get_current_blog_id() === $site_id ) {
    			$active = TRUE;
    		}
    
    		/**
    		* Get different possible language names
    		*
    		* @param  string $name Possible values:
    		*                      - 'native' (default) ex: Deutsch for German
    		*                      - 'english' English name of the language
    		*                      - 'http' ex: 'de-AT'
    		*                      - 'language_long' alias for 'http'.
    		*                      - 'language_short' first part of 'http', ex: 'de' in 'de-AT'
    		*                      - 'lang' alias for 'language_short'
    		*                      - 'wp_locale' Identifier for translation files used by WordPress
    		*                      - 'custom' Language name set in the site preferences
    		*                      - 'text' alias for 'custom'
    		* @return string
    		*/
    
    		$items[ $site_id ] = array(
    			'url'    => $url,
    			'code'   => $language->get_name( 'http' ),
    			'name'   => $language->get_name( 'language_short' ),
    			'active' => $active,
    		);
    	}
    
    	return $items;
    } 
    
    /**
     * Hook to add the language links in the menus
     **/
    add_action( 'mlp_and_wp_loaded', function() {
    	add_filter('wp_nav_menu_items', 'agm_add_links_menu_mlp4sf', 10, 2);
    });
    

    However, I see that the tags that the plugin includes in the <header> are incorrect. How could I correct it?

    Now it’s:

    
    <link rel="alternate" hreflang="es-EN" href="https://www.tvplay.es/products/">
    <link rel="alternate" hreflang="en-US" href="https://www.playoutautomationsoftware.com/products/">
    

    But it should be:

    
    <link rel="alternate" hreflang="es-EN" href="https://www.tvplay.es/venta-de-software-de-automatizacion-tv/">
    <link rel="alternate" hreflang="en-US" href="https://www.playoutautomationsoftware.com/products/">
    

    Thanks 4 all!

    Plugin Author dinamiko

    (@dinamiko)

    Hi Alejandro,

    For hreflang links you need to use and adpat mlp_linked_element_link filter, it allows you to modify not just MultilingialPress menu items links but also the hreflang links.

    You can use this example as starting point:
    https://wordpress.org/support/topic/problems-with-shop-permalinks/#post-9714350

    Thanks,
    Emili

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Bad redirect in WooCommerce Shop Page’ is closed to new replies.