Magento - 按类别获取可过滤的属性
我专门为网站创建了一个自定义导航模块,但我确实希望能够按特定类别列出可过滤属性。例如,我的主要导航是:
- 类别 1
- 类别 2
- 类别 3 等。
然后,当用户将鼠标悬停在某个类别上时,他们会看到一个带有一些可过滤选项的扩展菜单,例如:
类别 1
按制造商查看:
- 制造商 1
- 制造商 2
- 制造商 3 等。
我能够获取商店的所有可过滤属性,但我希望此列表仅提取每个类别的可过滤属性,例如类别 1 可能有不同的制造商类别 2。然后我需要缓存这些结果,因为这不会经常改变。
I have created a custom navigation module specifically for a website, but I really want to be able to list filterable attributes by a specific category. So for instance my main navigation is:
- Category 1
- Category 2
- Category 3 etc.
I then that when a user mouses over a category, they are then presented with an expanded menu with a few filterable options e.g.:
Category 1
View by manufacturer:
- Manufacturer 1
- Manufacturer 2
- Manufacturer 3 etc.
I am able to get all filterable attributes for the store, but I want this list to pull in only the filterable attributes per category, as for instance Category 1 may have different manufacturers to Category 2. I then need to cache these results as this will not change often.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乔给出的答案是一个很好的起点,但属性尚未返回任何选项。经过一番挫折后,我用下面的代码解决了这个问题。希望对大家有所帮助。
您自己需要做的唯一一件事就是使用 getValue() 函数创建正确的链接。
此代码已在 Magento 1.5 中测试
The answer that Joe gave was a good starting point, but the attributes didn't returned any options yet. After a lot of frustrations I solved the problem with the following code. Hope it helps all of you out.
The only thing you'll need to do yourself is create the correct link using the getValue() functions.
This code has been tested in Magento 1.5
Magento 使用模型
Catalog_Model_Layer
来完成此任务,所以我猜这可能是您最好的选择。买者自负,我还没有测试过这段代码:这里的每次迭代都会给你一个 Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute_Collection 类的对象,你应该能够在 foreach 循环中迭代它以获得所需的输出。
对于缓存,请尝试在站点上启用块缓存,并为块提供如下所示的缓存标记。 Magento 将缓存 HTML 输出,一切都会正常:
缓存仅对您传递的密钥有效,因此请确保,如果菜单要更改(例如,不刷新缓存),缓存键不同。
希望有帮助!
谢谢,
乔
Magento uses the model
Catalog_Model_Layer
to accomplish this, so I'm guessing this may be your best bet. Caveat emptor, I have not tested this code yet:Each iteration here will give you an object of the class
Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute_Collection
, which you should be able to iterate over in a foreach loop to get your desired output.For caching, try enabling block caching on your site and give the block a cache tag like the following. Magento will cache the HTML output and all will be right with the world:
The cache will only be valid for the key you pass, so make sure that, if the menu is to change (w/o flushing the cache, for instance), that the cache key is different.
Hope that helps!
Thanks,
Joe