2012-09-25 53 views
3

在Magento中,當用戶直接訪問Google等產品頁面時,麪包屑只會是「Home」 - >「Product Name」。以編程方式在Magento中添加麪包屑路徑?

How can I add categories in there even when users access the page directly from Google?

例如,在this page,我想在麪包中添加的類別「婚禮服飾」和「婚紗」。我想出了一個想法,而不是硬編輯breadcrumbs.phtml,但有沒有什麼辦法可以在template/catalog/product/view.phtml中以編程方式添加breadcrumbs項目?

我可以獲得當前產品的類別(標題和鏈接),然後使用一些函數/方法將它們動態地和編程地添加到麪包屑中。這可能嗎?

回答

4

下面是強制的Magento顯示完整的麪包屑,包括通過循環槽每個類別當前產品類別代碼:

©丹尼文斯

<?php 
if ($product = Mage::registry('current_product')) { 
    $categories = $product->getCategoryCollection()->load(); 

    if($categories) { 
     foreach ($categories as $category) 
     { 
      if($category) { 
      $category = Mage::getModel('catalog/category')->load($category->getId()); 
      break; 
      } 
     } 
    } 
    $lastCrumbName = $product->getName(); 
    $lastCategoryAdjust = 0; 
} 
else { 
    if($category = Mage::registry('current_category')) { 
    $lastCrumbName = $category->getName(); 
    } 
    $lastCategoryAdjust = 1; 
} 

if($category) { 
    if($path = $category->getPath()) { 
     $path = explode('/', $path); 
     $crumbs = array('home' => array('label' => 'Home', 
     'title' => 'Home', 
     'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB), 
     'first' => true, 
     'last' => false 
     )); 
     for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) { 
      $cur_category = Mage::getModel('catalog/category')->load($path[$i]); 
      if($cur_category && $cur_category->getIsActive()) { 
       $crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(), 
       'title' => $cur_category->getName(), 
       'link' => $cur_category->getUrl(), 
       'first' => false, 
       'last' => false 
       ); 
      } 
     } 
     $crumbs['current'] = array('label' => $lastCrumbName, 
     'title' => '', 
     'link' => '', 
     'first' => false, 
     'last' => true 
     ); 
    } 
} 
?> 
+0

感謝傑斯,所以我應該在哪裏把這個片段? breadcrumbs.phtml? view.phtml? –

+0

這段代碼應該被添加到你的breadcrumbs.phtml文件的頂部:app/design/frontend/default/template_name/template/page/html/ –

+0

我想這會增加頁面加載時間,因爲它不必要地循環整個分類路徑,如果這樣做能滿足我的需求,我可以在頁面加載時間上略微增加一些時間,但是請給我一個關於多少的想法?這會給我的網站帶來多大的服務器負擔和加載時間?非常感謝! –

相關問題