【问题标题】:How do I get the current catageory id?如何获取当前的类别 ID?
【发布时间】:2011-07-27 06:58:49
【问题描述】:

我有一个 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> 

我已经尝试获取我在布局中设置的 ID,但没有这样的运气。我试过了:

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

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

但是这些方法都不起作用。有谁知道我如何获得身份证?

【问题讨论】:

    标签: php xml variables layout magento


    【解决方案1】:

    我认为这是最好的方法;)

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

    【讨论】:

    • 这是正确答案,应该这样标记:)
    • @ShivSingh 基本上是上面的脚本来获取上次访问的类别
    【解决方案2】:

    没试过,但可能是这样的:

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

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

    干杯,
    京东

    【讨论】:

    • 感谢@Jonathan Day,它对我有用。 Mage::registry('current_category')-&gt;getId();当我们从cms页面或cms静态块中调用产品时,此功能不起作用。
    【解决方案3】:

    试试下面的代码

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

    【讨论】:

      【解决方案4】:

      您是否考虑过更新目录页面的布局而不是制作 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>
      

      您可能可以使用 google 来了解有关如何布局的更多信息。

      【讨论】:

        【解决方案5】:

        这对我有用:

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

        【讨论】:

          【解决方案6】:

          这对我有用:

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

          然后你将当前类别作为一个对象,你可以通过以下方式获取 id:

          $currentCat->getId();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-08
            • 1970-01-01
            • 2016-02-23
            • 2016-03-31
            • 2016-04-25
            • 1970-01-01
            • 2014-12-11
            • 1970-01-01
            相关资源
            最近更新 更多