2011-03-31 59 views
5

我有一個CMS頁面,我要對顯示產品的下列更新XML代碼:如何獲取當前catageory id?

<reference name="content"> 
    <block type="catalog/product_list" name="product_list" template="catalog/product/wholesale-list.phtml"> 
     <action method="setCategoryId"><category_id>191</category_id></action> 
     <action method="setToolbarBlockName"><name>product_list_toolbar</name></action> 
    </block> 
</reference> 

我試圖讓我在佈局中設置的ID,但沒有這樣的運氣。我曾嘗試:

$_category = Mage::registry(‘current_category’); 
$currentCategoryId= $_category->getId(); 

$layer = Mage::getSingleton(‘catalog/layer’); 
$_category = $layer->getCurrentCategory(); 
$currentCategoryId= $_category->getId(); 

但是這些方法都沒有工作。有誰知道我如何獲得ID?

回答

4

沒有試過,但也許是這樣的:

$this->getLayout()->getBlock('product_list')->getCategoryId() 

這樣,你直接讓你已經在XML塊對象上設置的變量。

乾杯,
JD

+0

太謝謝你了! – Chris 2011-04-01 14:20:58

1

您是否考慮更新目錄頁面的佈局而不是創建CMS頁面?我想有些情況下你可能更喜歡CMS頁面,但是你可以很容易地更新類別的佈局,就像它在Magento中一樣容易,這並不容易:)

登錄到管理後臺,轉至目錄 - >管理類別,然後選擇所需的類別,然後單擊自定義設計選項卡。注意自定義佈局更新字段。這是您可以放置​​佈局更新的位置。

所以對於這一類,如果你不想顯示特定塊,你可以做類似

<reference name="right"> 
     <remove name="right.permanent.callout" /> 
</reference> 

這將移除佈局完全right.permanent.callout命名塊。如果你想只改變了產品上市使用特定PHTML文件,你可以這樣做......

<reference name="product_list"> 
     <action method="setTemplate"><template>catalog/product/wholesale.phtml</template></action> 
</reference> 

你或許可以使用谷歌找到更多關於如何佈局。

0

這個工作對我來說:

$layer = Mage::getSingleton('catalog/layer'); 
$_category = $layer->getCurrentCategory(); 
$currentCategoryId= $_category->getId(); 
39

我認爲這是最好的辦法;)

Mage::registry('current_category')->getId(); 
+0

這是正確的答案,應該標記爲:) – nickspiel 2015-04-29 03:08:01

3

試試下面的代碼

Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
0

把這個工作對我來說:

$currentCat = $this->getLayout()->getBlock('category.products')->getCurrentCategory(); 

然後你當前類別作爲對象,你可以得到ID:

$currentCat->getId();