2013-02-04 97 views
2

我有一家擁有2家語言商店的magento(企業)網站。每個用戶都有自己的指定項目或資源的專用url,可以是靜態頁面或產品頁面。語言商店特定網址上的Magento 404錯誤

我通過CMS使用URL規則來管理所有資源的SEF URL。

的問題是以下情形:

  1. 網站默認爲LANG#1。

  2. 當用戶從LANG#1切換到LANG#2時,切換沒有問題 - 切換到特定語言(_store = lang>> http://www.sitename.com/?_store=lang)

  3. 但不管什麼郎店我在,如果我有選擇從其他郎商店網址爲我目前的語言專賣店,我得到一個404錯誤。

我想什麼系統檢查當前存儲的資源請求,如果沒有找到,它應該路由到下一個存儲並檢查那裏的資源,如果找到了,存儲應該切換到langsto該項目被發現和網址重定向。

爲了達到這個目的,我應該延伸什麼類(我對magento來說是比較新的)。

我去就檢查,如果我能擴展這個類做我想做的:/app/code/core/Mage/Core/Model/Mysql4/Url/Rewrite.php

但不能肯定是否我處於這樣的要求的正確位置。

任何幫助將不勝感激!

謝謝

回答

1

我能夠解決這個問題了。我所做的只是擴展了/app/code/core/Mage/Core/Model/Mysql4/Url/Rewrite.php文件。

因此,我過去loadByRequestPath函數爲當前存儲中的請求提供服務,如果找不到,則獲取所有其他可用存儲的列表,循環並查找該項是否存在。如果發現重定向到存在該產品和whola的URL密鑰的商店。

如果沒有商店有它,那麼返回404錯誤。

對於靜態頁面,當您切換商店/語言時,URL重寫管理器將能夠爲您解決問題。

我希望這可以幫助別人!

+0

能否請你加入循環代碼爲您loadByRequestPath功能? – Paktas

+2

你想分享的代碼,因爲我有同樣的magento安裝5 +商店類似的問題。 –

0

我不認爲我的是最優雅的解決方案,但我會發布它,因爲它爲我工作。 基本上我在所有商店中搜索我正在尋找的路徑而不是指定的商店。任何建議都會受到歡迎。

我的config.xml

<?xml version="1.0"?> 
<config> 
<global> 
    <modules> 
    <Soipo_UrlRewrite> 
     <version>0.1</version> 
    </Soipo_UrlRewrite> 
    </modules> 
    <models> 
    <soipo_urlrewrite> 
     <class>Soipo_UrlRewrite_Model</class> 
    </soipo_urlrewrite> 
    <core_mysql4> 
     <rewrite> 
      <url_rewrite>Soipo_UrlRewrite_Model_Mage_Core_Model_Mysql4_Url_Rewrite</url_rewrite> 
     </rewrite> 
    </core_mysql4> 
    </models> 
</global> 
</config> 

重寫。PHP

類Soipo_UrlRewrite_Model_Mage_Core_Model_Mysql4_Url_Rewrite擴展Mage_Core_Model_Mysql4_Url_Rewrite {

/** 
* This function get an array of store ids, containing the Admin store. 
* @return array 
*/ 
public function getStoreIds(){ 
    $allStores = Mage::app()->getStores(); 
    $storeIds = array(); 
    $storeIds[] = Mage_Core_Model_App::ADMIN_STORE_ID; 
    foreach ($allStores as $_eachStoreId => $val) 
    { 
     $_storeId = Mage::app()->getStore($_eachStoreId)->getId(); 
     $storeIds[] = $_storeId; 
    } 
    return $storeIds; 
} 

/** 
* Load rewrite information for request 
* If $path is array - we must load all possible records and choose one matching earlier record in array 
* 
* @param Mage_Core_Model_Url_Rewrite $object 
* @param array|string $path 
* @return Mage_Core_Model_Resource_Url_Rewrite 
*/ 
public function loadByRequestPath(Mage_Core_Model_Url_Rewrite $object, $path) 
{ 

    if (!is_array($path)) { 
     $path = array($path); 
    } 

    $pathBind = array(); 
    foreach ($path as $key => $url) { 
     $pathBind['path' . $key] = $url; 
    } 

    $storeIds = $this->getStoreIds(); 


    // Form select 
    $adapter = $this->_getReadAdapter(); 
    $select = $adapter->select() 
     ->from($this->getMainTable()) 
     ->where('request_path IN (:' . implode(', :', array_flip($pathBind)) . ')') 
     ->where('store_id IN(?)', $storeIds); 

    $items = $adapter->fetchAll($select, $pathBind); 


    // Go through all found records and choose one with lowest penalty - earlier path in array, concrete store 
    $mapPenalty = array_flip(array_values($path)); // we got mapping array(path => index), lower index - better 
    $currentPenalty = null; 
    $foundItem = null; 
    foreach ($items as $item) { 
     if (!array_key_exists($item['request_path'], $mapPenalty)) { 
      continue; 
     } 
     $penalty = $mapPenalty[$item['request_path']] << 1 + ($item['store_id'] ? 0 : 1); 
     if (!$foundItem || $currentPenalty > $penalty) { 
      $foundItem = $item; 
      $currentPenalty = $penalty; 
      if (!$currentPenalty) { 

       break; // Found best matching item with zero penalty, no reason to continue 
      } 
     } 
    } 

    // Set data and finish loading 
    if ($foundItem) { 
     $object->setData($foundItem); 
    } 

    // Finish 
    $this->unserializeFields($object); 
    $this->_afterLoad($object); 

    return $this; 
} 

}