2012-06-01 54 views
0

我試圖找到確保Magento的總是顯示每一個產品,包括類目路徑的完整URL的解決方案來獲得完整的產品網址。 我想要完整的網址也在搜索結果中。 對於這個我已經把每一個產品只在一個類別。Magento的:如何使用

可以通過這個URL來完成通過Magento的自定義改寫? 是301重定向使用.htaccess,爲/product.html類別/子類別/ product.html一個好主意?

感謝 穆迪

回答

4

,我們做的就是在產品網址全品類什麼是使用輔助函數會試圖讓你與該類別產品的URL。您也可以重寫產品方法,但是當另一個模塊重寫某些產品時它會產生痛苦,這就是爲什麼我們使用助手方法的原因。

這是我們的方法:

public static function getFullUrl (Mage_Catalog_Model_Product $product , 
     Mage_Catalog_Model_Category $category = null , 
     $mustBeIncludedInNavigation = true){ 

    // Try to find url matching provided category 
    if($category != null){ 
     // Category is no match then we'll try to find some other category later 
     if(!in_array($product->getId() , $category->getProductCollection()->getAllIds()) 
       || !self::isCategoryAcceptable($category , $mustBeIncludedInNavigation)){ 
      $category = null; 
     } 
    } 
    if ($category == null) { 
     if(is_null($product->getCategoryIds())){ 
      return $product->getProductUrl(); 
     } 
     $catCount = 0; 
     $productCategories = $product->getCategoryIds(); 
     // Go through all product's categories 
     while($catCount < count($productCategories) && $category == null) { 
      $tmpCategory = Mage::getModel('catalog/category')->load($productCategories[$catCount]); 
      // See if category fits (active, url key, included in menu) 
      if (!self::isCategoryAcceptable($tmpCategory , $mustBeIncludedInNavigation)) { 
       $catCount++; 
      }else{ 
       $category = Mage::getModel('catalog/category')->load($productCategories[$catCount]); 
      } 
     } 
    } 
    $url = (!is_null($product->getUrlPath($category))) ? Mage::getBaseUrl() . $product->getUrlPath($category) : $product->getProductUrl(); 
    return $url; 
} 

/** 
* Checks if a category matches criteria: active && url_key not null && included in menu if it has to 
*/ 
protected static function isCategoryAcceptable(Mage_Catalog_Model_Category $category = null, $mustBeIncludedInNavigation = true){ 
    if(!$category->getIsActive() || is_null($category->getUrlKey()) 
     || ($mustBeIncludedInNavigation && !$category->getIncludeInMenu())){ 
     return false; 
    } 
    return true; 
} 

如果一類是指定它會嘗試獲取相對於這一個網址。

如果沒有指定類別或找不到提供的url,該方法會嘗試獲取產品URL相對於產品所附的第一個類別,並檢查它是否可接受(活動,帶有url密鑰和匹配的導航標準)。

最後,如果回落到原來的$product->getProductUrl() Magento的方法。

你必須在模板(類別,車中的產品,最近觀看等...)這個呼叫時使用:

echo $this->helper('yourcompany/yourmodule')::getFullProductUrl($_product); 

編輯:

我把扎卡里的言論考慮到並通過添加一些檢查和選項來調整它。希望現在很酷。 例子:

echo $this->helper('yourcompany/yourmodule')::getFullProductUrl($_product, $aCategory); 

將試圖找到在$ aCategory產品網址,然後回落到其他類別的URL,最終產品的基礎URL

echo $this->helper('yourcompany/yourmodule')::getFullProductUrl($_product, someCategory, false); 

也會考慮不包括在導航類別。

+0

我用類似的東西,但你正在一個錯誤是,你假設getUrlPath()會返回一個值。它並不總是在某些情況下。不幸的是,您也不能依賴具有url密鑰的類別。在$ url聲明中使用它之前,您必須先進行檢查以確保該類別有一個。 (如果沒有,重複到下一個類別) –

+0

這是真的。感謝您的輸入,我會很快修改答案! – baoutch

+0

感謝球員們對我們的反饋意見......我們會盡快實施。再次感謝.. – Moody