2015-08-09 115 views
0

我需要在產品列表頁面中添加CMS頁面,僅限特定sub-sub-category = 35。我嘗試的方法是編輯view.phtmlmagento在產品列表頁面中獲取子子類別ID

echo $this->getLayout()->createBlock('cms/block')->setBlockId('adminCMSid')->toHtml() 

但我需要在view.phtml進行比較,如果當前的產品具有細分子類別等於35 有人能幫助我如何獲得當前的細分子類別ID和解決問題?

回答

2

簡短的回答:

catalog/view.phtml

<?php 
    $_categories = $_product->getCategoryIds(); 

    foreach($_categories as $category){ 
     if($category == 35){ 
      echo $this->getLayout()->createBlock('cms/block')->setBlockId('adminCMSid')->toHtml(); 
      break; //stop the loop 
     } 
    } 
?> 

龍答:

如果你想找到當前可用模板文件中的數據例如在的view.phtml中你可以插入:

<?php Mage::log(print_r($_product->debug(), true), null, 'finddata.log', true); ?> 

刷新產品頁面,然後進入var/logs文件夾,你會發現finddata.log文件。 在那裏您可以看到該產品的類別以及可以使用的陣列詳細信息。

然後,你可以做這樣的事情:

$_categories = $_product->getCategoryIds(); 
var_dump($_categories); 

這會給你類別,鑑於產品的陣列。像這樣,測試和找出你的方式更容易。

+0

謝謝Claudiu。它像一個魅力! – abagray

0
<?php $_categories = $this->getCurrentChildCategories() ?> 

<?php foreach ($_categories as $_category): ?> 
    <?php if ($_category->getId() == 35) { 
     //Subcategory id equals 35 
    }?> 
<?php endforeach; ?> 
+0

我沒有得到它與$ this-> getCurrentChildCategories()。 – abagray

+0

在審查這個你必須在目錄/產品/ list.phtml讓這個工作。只是Claudiu在上面發佈的另一種方法,你會發現磁電有許多美妙的方法,希望這可以幫助別人在未來:-) – iamgraeme

相關問題