2012-09-14 29 views
11

在Magento中,我們現在擁有的是,單個產品頁面上的麪包屑總是根據用戶到達頁面的方式而變化。例如,如果用戶從頂級類別一直點擊到子類別,然後再點擊到產品,則麪包屑將類似於「家>>頂級類別>>子類別>>產品」。在Magento的各個產品頁面上製作一致的麪包屑?

但是,如果用戶在產品頁面直接到達,如谷歌查詢,麪包屑,簡直是「首頁>>產品」。像這樣,並不令人滿意。

即使用戶到達Google的頁面,通過在麪包屑中添加類別也肯定會改善用戶體驗。而且它肯定會增加每次訪問的PV,因爲用戶可能會點擊麪包屑中的父類別。

那麼有沒有什麼辦法讓它保持一致並始終在產品頁面上顯示類別路徑?

我的想法是編輯page/html/breadcrumbs.phtml,但我不知道如何將類別添加到麪包屑只在產品頁面上。

任何幫助,將不勝感激!

======================================

編輯:在單詞,無論用戶如何到達產品頁面,我只想讓類別出現在產品頁面上的麪包屑中。只要類別在哪裏,哪些類別無關緊要。

任何人有任何想法?這很難嗎?

關閉我的頭頂,我將需要編輯麪包屑模板,並在其中注入類別,如果沒有任何,它是產品頁面......但我不知道如何,我可以「T似乎找到任何相關的解決方案,這在任何地方......

+0

我不認爲你的想法是每個人都會想要的。很多網站在不同類別下都有相同的產品,因此使用您的邏輯麪包屑會導致錯誤的信息。對於您的解決方案,我建議您使用自己的自定義塊更改原始的Breadcrumb塊,並在其中顯示產品屬性等內容。 –

+1

感謝您的評論,但我認爲這絕對是每個人都想要的想法。對不起,我沒有說清楚。我只需要在那裏的類別,在麪包屑中,哪些類別無關緊要; Magento可以自己決定。現在的問題是,我的大多數訪問者甚至都沒有看到麪包屑中的任何類別,因爲它們都是從谷歌到達並直接登陸產品頁面......它既不便於用戶使用,也不適合頁面瀏覽。在直接訪問時,可能會默認顯示ID最低的類別?或隨機?兩者都很好。 –

回答

16

用這個替換「app/code/core/Mage/Page/Block/Html/Breadcrumbs.php」中的_toHtml()函數。

protected function _toHtml() {    

    $cat_id = ""; 

    if (Mage::registry('current_product')) { 
     $product_id = Mage::registry('current_product')->getId(); 
     $obj = Mage::getModel('catalog/product'); 
     $_product = $obj->load($product_id); // Enter your Product Id in $product_id 

     if ($product_id) { 
     $categoryIds = $_product->getCategoryIds(); 
     $cat_id = $categoryIds[0]; 
     } 

     $category = Mage::getModel('catalog/category')->load($cat_id); 
     $cat_name = $category->getName(); 
     $cat_url = $this->getBaseUrl().$category->getUrlPath(); 
    } 

    if (is_array($this->_crumbs)) { 
     reset($this->_crumbs); 
     $this->_crumbs[key($this->_crumbs)]['first'] = true; 
     end($this->_crumbs); 
     $this->_crumbs[key($this->_crumbs)]['last'] = true; 
    } 

    if($cat_id) { 
     $this->_crumbs['category'.$cat_id] = array('label'=>$cat_name, 'title'=>'', 'link'=>$cat_url,'first'=>'','last'=>'','readonly'=>''); 
     ksort($this->_crumbs); 
     $home = $this->_crumbs['home']; 
     unset($this->_crumbs['home']); 
     array_unshift($this->_crumbs,$home); 
    } 

    $this->assign('crumbs', $this->_crumbs); 
    return parent::_toHtml(); 
} 
+0

+1將它保留在它所屬的Page/Block/Html/Breadcrumbs.php中,而不是執行標準Magento模板Trashcan編程我正在從中恢復我們的網站。很容易模塊化。 –

+0

如何在不修改核心文件的情況下做到這一點? – Raffael

2

我完全理解你想要做什麼,我已經注意到了,甚至從Magento的自己的搜索鏈接的產品將失去它的類麪包屑。我同意Slayer,我只會爲產品頁面實現您自己的自定義塊。

您可以使用以下代碼檢索產品類別樹。我已經測試了這個產品(在Magento CE 1.7.0.2中),只有一個類別層次結構的產品和那些具有多個類別層次結構的產品。

注意:你可能會想要做這個代碼的一些工作,因爲它的裝載每一類對象到一個數組,這可能成爲相當內存密集型。我會建議將只需到一個關聯數組的信息,然後補充說陣列主陣列

$productCategories = array(); 

// grab the products category id array...we only want one, so grab the first one 
$categoryIds = $_product->getCategoryIds(); 
$categoryId = $categoryIds[0]; 

// we don't want the root category as the name may not be user friendly 
while ($categoryId != Mage::app()->getStore()->getRootCategoryId()) 
{ 
    // load the category object and add it to the product categories array 
    $currentCategory = Mage::getModel('catalog/category')->load($categoryId); 
    $productCategories[] = $currentCategory; 
} 

// as the categories were added in reverse order we'll need to "unreverse" them 
foreach (array_reverse($productCategories) as $category) 
{ 
    echo $category->getName().'/'; 
} 
+0

我不認爲這會給麪包屑添加任何東西嗎? –

+0

不,你說得對,這不會添加到麪包屑,這是我用來複制面包屑的代碼。然後,您可以創建自己的模塊,以在產品視圖頁面上顯示麪包屑。 – CCBlackburn

+0

此代碼不必要地加載整個類別模型(並在此循環中)。對此有更有效的方法;上面發佈的代碼只會增加頁面加載時間。 –

3

您可以改寫的產品型號來改變getProductUrl方法。基本上獲得該產品的第一類並在產品路徑之前附加類別路徑。

0

@jacek_podwysocki發佈了一個解決方案,將一個片段添加到breadcrumbs.phtml,它的工作原理。

看到這裏的代碼:Programmatically add breadcrumbs paths in Magento?

就在這個片段添加到應用程序/設計/前端/默認/ TEMPLATE_NAME /模板/頁/ HTML/breadcrumbs.phtml

添加的頁面加載頂部/根據microtime(),PHP渲染時間僅爲0.05秒。

1

查找應用程序/代碼/核心/法師/目錄/助手/ Data.php和Data.php複製到 應用程序/代碼/本地/法師/目錄/助手/ Data.php 查找功能公衆功能getBreadcrumbPath(),下面的代碼添加到該函數

if ($this->getProduct() && !$this->getCategory()) { 
     $_categoryIds = $this->getProduct()->getCategoryIds(); 

     if ($_categoryId = $_categoryIds[0]) { 
      $_category = Mage::getModel('catalog/category')- >load($_categoryId); 
      Mage::register('current_category', $_category); 
     } 

    } 
+0

對於多層類別不起作用(至少在magento 1.9上)。 –

相關問題