Magento multilanguage - double change in language resuts in 404 (or how to change language within stores not views)

人走茶凉 提交于 2019-12-05 18:51:28

There is no such thing in the native Magento as far as I know.
That said, you can use the following code to get the current page URL for each store.

$resource = Mage::getSingleton('core/resource');
$requestPath = Mage::getSingleton('core/url')->escape(
    trim(Mage::app()->getRequest()->getRequestString(), '/')
);

$select = $resource->getConnection('default_read')->select()
    ->from(array('c' => $resource->getTableName('core/url_rewrite')), '')
    ->where('c.request_path=?', $requestPath)
    ->where('c.store_id=?', Mage::app()->getStore()->getId())
    ->joinInner(
        array('t' => $resource->getTableName('core/url_rewrite')),
        "t.category_id=c.category_id AND t.product_id=c.product_id AND t.id_path=c.id_path",
        array('t.store_id', 't.request_path')
    );
$storeUrls = (array) $resource->getConnection('default_read')
    ->fetchPairs($select);

This will give you an array with the array key being the store IDs and the array values being the request path after the Magento base URL, e.g. assuming your French store has the ID 1 and the German one has the ID 2, you would get:

Array
(
    [1] => cartouches-refilables.html
    [2] => nachfullpatronen.html
)

Then, in the foreach loop where the URL for each store is output, use

<?php $url = isset($storeUrls[$_lang->getId()]) ? $_lang->getUrl($storeUrls[$_lang->getId()]) : $_lang->getCurrentUrl() ?>

The call to $_lang->getUrl() will add the base URL, so you will get the full URL for each store (e.g. http://cissmarket.com/de/nachfullpatronen.html). If no store view value is found in the core_url_rewrite table it will revert to the default behaviour.

You still need the ___store=fr query parameter because otherwise Magento will think you are trying to access the new path in the context of the old store. Luckily, the getUrl() call an the store model adds that for you automatically.

The code querying the database can be anywhere of course (since its PHP), even in the template, but please don't put it there. The correct place to have code that access the database is a resource model. I suggest you create a resource model and put it in a method there.

Turko Digital Marketing

I've found an ugly patch until a better approach comes up.

In the admin section, i've added the following javascript inside the wysiwyg in CMS > PAGES > (My 404 pages) (at the beginning of the wysiwyg) :

<script type="text/javascript" language="javascript">// <![CDATA[
var lang = "en";
var rooturl = "{{config path="web/unsecure/base_url"}}"
var url = document.location.href;
if(!(url.match("/"+lang+"/")))
{
  var newUrl = url.replace(rooturl , rooturl+lang+"/" );
  window.location.href = newUrl;
}
// ]]></script>

(Note: you need to do this for all of your translated 404 pages. In each 404 page you need to modify lang="en" for your storeview url value)

Because the wysiwyg (tiny_mce) does not allow javascript to be threated, you'll have to modify js/mage/adminhtml/wysiwyg/tinymce/setup.js. Add the following code under line 97 (under "var settings = "):

extended_valid_elements : 'script[language|type|src]',
Endur

For Magento 1.7.0.2 and 1.8.0.0 this is the bugfix:

Starting from line 251 of /app/code/core/Mage/Core/Model/Url/Rewrite.php

:

Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $currentStore->getCode(), true);

// endur 02-03-2013 fix for missed store code

// $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();

if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()>getCode()) { 
    $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getRequestPath();
    } else {
      $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();
    }

// endur 02-03-2013 end

Make sure to create a custom copy of the file in: /app/code/local/Mage/Core/Model/Url/Rewrite.php or: /app/code/local/YourTheme/Mage/Core/Model/Url/Rewrite.php

source:

It looks like a bug in Magento 1.7. Here is a hack that worked for me. It should work for a two language store with store code in URL

in var/www/html/shop1/app/code/core/Mage/Core/Model/Url/Rewrite.php

remove this line

 // $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();

and add these:

 $storecode = Mage::app()->getStore()->getCode();
 if ($storecode='en')
 {
    $targetUrl = $request->getBaseUrl(). '/'.$storecode.'/' . $this->getRequestPath();
 }
 else
 {
    $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();
 }
panticz

Here a another solution for this problem. Just add this code after "$this->load($pathInfo, 'request_path');" in app/code/core/Mage/Core/Model/Url/Rewrite.php:

        if (!$this->getId() && !isset($_GET['___from_store'])) {
            $db = Mage::getSingleton('core/resource')->getConnection('default_read');
            $result = $db->query('select store_id from core_url_rewrite WHERE request_path = "' . $pathInfo . '"');
            if ($result) {
                $storeIds = array();
                if($row = $result->fetch(PDO::FETCH_ASSOC)) {
                    $storeId = $row['store_id'];
                    $storeCode = Mage::app()->getStore($storeId)->getCode();

                    header("HTTP/1.1 301 Moved Permanently");
                    header("Location: http://" . $_SERVER['HTTP_HOST'] . "/" . $pathInfo . "?___store=" . $storeCode);
                    exit();
                }
            }
        }

guys. For this error there is magento module. It have rewrite 2 models http://www.magentocommerce.com/magento-connect/fix-404-error-in-language-switching.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!