如何获得当前的分类id?

时间:2022-11-26 07:28:31

I have a CMS page that I am going to display products on with the following updated XML code:

我有一个CMS页面,我将用以下更新后的XML代码显示产品:

<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> 

I have tried getting the ID I set in the layout, but no such luck. I have tried:

我试过在布局中设置ID,但是没有这样的运气。我有尝试:

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

and

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

But neither of these methods work. Does anyone know how I can get the ID?

但这两种方法都不起作用。有人知道我怎样才能拿到身份证吗?

6 个解决方案

#1


4  

Haven't tried this, but maybe something like:

我还没试过,但可能是这样:

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

This way you are directly getting the variable that you have set on the Block object in the XML.

这样,您就可以直接获得在XML中的块对象上设置的变量。

Cheers,
JD

欢呼,JD

#2


38  

i think this is the best way ;)

我认为这是最好的办法;

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

#3


3  

Try below code

尝试下面代码

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

#4


1  

Have you considered updating the layout of the catalog page rather than making a CMS page? I suppose there are situations where you might prefer a CMS page, but you can update the layout of a category fairly easily, well about as easy as it gets in Magento, which isn't all that easy :)

您是否考虑过更新目录页面的布局而不是创建一个CMS页面?我想有些情况下你可能更喜欢CMS页面,但是你可以很容易地更新一个类别的布局,就像Magento (Magento)中一样简单,这并不容易:)

Login to the admin backend, go to Catalog -> Manage Categories, then choose the category you want, then click the Custom Design tab. Notice the Custom Layout Updates field. This is where you can put your layout updates.

登录到管理后端,进入Catalog ->管理类别,然后选择您想要的类别,然后单击Custom Design选项卡。注意自定义布局更新字段。这是您可以放置布局更新的地方。

So for this category, if you didn't want to display a specific block you could do something like

对于这个类别,如果你不想显示一个特定的块你可以做一些类似的事情

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

Which would remove the block named right.permanent.callout from the layout altogether. And if you wanted to just change the product listing to use your specific phtml file you could do something like...

这将删除名为right.permanent的块。从布局中完全调用。如果您只想更改产品清单以使用特定的phtml文件,您可以做以下事情:

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

You can probably use google to find out more about how to layouts.

您可以使用谷歌来了解更多关于如何布局的信息。

#5


1  

This works for me:

这工作对我来说:

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

#6


0  

This worked for me:

这工作对我来说:

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

Then you have the current category as an object and you can get the id by:

然后你将当前类别作为对象,你可以通过:

$currentCat->getId();

#1


4  

Haven't tried this, but maybe something like:

我还没试过,但可能是这样:

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

This way you are directly getting the variable that you have set on the Block object in the XML.

这样,您就可以直接获得在XML中的块对象上设置的变量。

Cheers,
JD

欢呼,JD

#2


38  

i think this is the best way ;)

我认为这是最好的办法;

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

#3


3  

Try below code

尝试下面代码

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

#4


1  

Have you considered updating the layout of the catalog page rather than making a CMS page? I suppose there are situations where you might prefer a CMS page, but you can update the layout of a category fairly easily, well about as easy as it gets in Magento, which isn't all that easy :)

您是否考虑过更新目录页面的布局而不是创建一个CMS页面?我想有些情况下你可能更喜欢CMS页面,但是你可以很容易地更新一个类别的布局,就像Magento (Magento)中一样简单,这并不容易:)

Login to the admin backend, go to Catalog -> Manage Categories, then choose the category you want, then click the Custom Design tab. Notice the Custom Layout Updates field. This is where you can put your layout updates.

登录到管理后端,进入Catalog ->管理类别,然后选择您想要的类别,然后单击Custom Design选项卡。注意自定义布局更新字段。这是您可以放置布局更新的地方。

So for this category, if you didn't want to display a specific block you could do something like

对于这个类别,如果你不想显示一个特定的块你可以做一些类似的事情

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

Which would remove the block named right.permanent.callout from the layout altogether. And if you wanted to just change the product listing to use your specific phtml file you could do something like...

这将删除名为right.permanent的块。从布局中完全调用。如果您只想更改产品清单以使用特定的phtml文件,您可以做以下事情:

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

You can probably use google to find out more about how to layouts.

您可以使用谷歌来了解更多关于如何布局的信息。

#5


1  

This works for me:

这工作对我来说:

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

#6


0  

This worked for me:

这工作对我来说:

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

Then you have the current category as an object and you can get the id by:

然后你将当前类别作为对象,你可以通过:

$currentCat->getId();