getStoreCategories() 不返回任何内容

发布于 2024-10-02 01:28:32 字数 1352 浏览 2 评论 0原文

我目前正在学习 Magento,似乎遇到了很多处于我职位的人都熟悉的问题,但给出的解决方案都不适合我。我希望用根类别中找到的所有类别填充导航菜单,但我必须执行此操作的代码段不起作用。以下是应该执行此类任务的代码摘录:

<div id="utilities">
    <?php $_menu = ''?>
    <?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php $_menu .= $this->drawItem($_category) ?>
    <?php endforeach ?>
    <?php if ($_menu): ?>
    <div class="nav-container">

        <ul id="nav">

            <?php $_anyActive = false; foreach ($this->getStoreCategories() as $_category) { $_anyActive = $_anyActive || $this->isCategoryActive($_category); } ?>
             <li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><a href="<?php echo $this->getUrl('')?>"><span><?php echo $this->__('Home') ?></span></a></li> 

            <?php echo $_menu; ?>
        </ul>

    </div>
    <?php endif; ?>
</div>

现在,我已经对此进行了相当广泛的故障排除,并开始在 $this-?getStoreCategories() 上执行 var_dump() ,但这返回了 NULL。我知道该语句没有比该方法更进一步,因此可以得出结论,错误在于这行代码:

<?php foreach ($this->getStoreCategories() as $_category): ?>

我尝试创建新的根类别并用子类别填充它们(也填充产品),然后更改根类别管理员设置到这个新创建的,仍然无济于事。我知道这段代码被拉入页面,就像我在打印到屏幕上的违规语句之前输入静态文本一样。

至少可以说我很惊讶。任何帮助将不胜感激,并提前非常感谢您!

I'm currently learning Magento and seem to have come across a problem that is familiar with a lot of people in my position, yet none of the given solutions work for me. I am looking to populate the navigation menu with all the categories found in the root category but the piece of code I have to do this does not work. Here is the excerpt of code taken which should perform such a task:

<div id="utilities">
    <?php $_menu = ''?>
    <?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php $_menu .= $this->drawItem($_category) ?>
    <?php endforeach ?>
    <?php if ($_menu): ?>
    <div class="nav-container">

        <ul id="nav">

            <?php $_anyActive = false; foreach ($this->getStoreCategories() as $_category) { $_anyActive = $_anyActive || $this->isCategoryActive($_category); } ?>
             <li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><a href="<?php echo $this->getUrl('')?>"><span><?php echo $this->__('Home') ?></span></a></li> 

            <?php echo $_menu; ?>
        </ul>

    </div>
    <?php endif; ?>
</div>

Now, I've troubleshooted this quite extensively, and started by doing a var_dump() on the $this-?getStoreCategories() but this returned a NULL. I know that the statement does not get any further than that method so can conclude that the error lies in this line of code:

<?php foreach ($this->getStoreCategories() as $_category): ?>

I have tried creating new root categories and populating these with subcategories (also filling with products) then changing the root category in the admin settings to this newly created one, still to no avail. I know this code is being pulled into the page as if I type static text before the offending statement it prints to the screen.

I'm flabbergasted to say the least. Any help would be so much appreciated, and thank you so much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2024-10-09 01:28:32

您的问题是您使用的块没有 getStoreCategories() 方法。 $this - 是对您手动创建的块的引用。

Magento 具有高度抽象性,因此这里的每个问题都可以通过不同的方式解决。

使用:

Mage::helper('catalog/category')->getStoreCategories()

而不是:

$this->getStoreCategories()

当它起作用时 - 更好地在您的块中创建 getStoreCategories() 并将此代码移动到其中 - 正如 Magento 架构所设想的那样。

Your problem is that you're using a Block, that has no getStoreCategories() method. $this - is a reference to your manually created block.

Magento has high level of abstraction so every problem here can be solved in different ways.

Use:

Mage::helper('catalog/category')->getStoreCategories()

Instead of:

$this->getStoreCategories()

When it works - better create getStoreCategories() in your block and move this code to it - as supposed by Magento architecture.

阿楠 2024-10-09 01:28:32

获取类别菜单的推荐方法是使用方法 Mage_Catalog_Block_Navigation::renderCategoriesMenuHtml()

因此,对于 Mage_Catalog_Block_Navigation 或其派生的任何块都可以使用调用该方法的模板 app/code/design/base/default/template/navigation/top.phtml

The recommended way to get a menu of categories is with the method Mage_Catalog_Block_Navigation::renderCategoriesMenuHtml().

So for any block that is Mage_Catalog_Block_Navigation or descends from it can use the template app/code/design/base/default/template/navigation/top.phtml which calls that method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文