Magento - 按类别获取可过滤的属性

发布于 2024-09-08 04:34:01 字数 402 浏览 6 评论 0原文

我专门为网站创建了一个自定义导航模块,但我确实希望能够按特定类别列出可过滤属性。例如,我的主要导航是:

  • 类别 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 技术交流群。

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

发布评论

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

评论(2

思念满溢 2024-09-15 04:34:01

乔给出的答案是一个很好的起点,但属性尚未返回任何选项。经过一番挫折后,我用下面的代码解决了这个问题。希望对大家有所帮助。

$layer = Mage::getModel("catalog/layer");
foreach($categories as $categoryid) {
    $category = Mage::getModel("catalog/category")->load($categoryid);
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();

    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'price') {
            $filterBlockName = 'catalog/layer_filter_price';
        } elseif ($attribute->getBackendType() == 'decimal') {
            $filterBlockName = 'catalog/layer_filter_decimal';
        } else {
            $filterBlockName = 'catalog/layer_filter_attribute';
        }

        $result = $this->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();

        foreach($result->getItems() as $option) {
            echo $option->getLabel().'<br/>';
            echo $option->getValue();
        }
}

您自己需要做的唯一一件事就是使用 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.

$layer = Mage::getModel("catalog/layer");
foreach($categories as $categoryid) {
    $category = Mage::getModel("catalog/category")->load($categoryid);
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();

    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() == 'price') {
            $filterBlockName = 'catalog/layer_filter_price';
        } elseif ($attribute->getBackendType() == 'decimal') {
            $filterBlockName = 'catalog/layer_filter_decimal';
        } else {
            $filterBlockName = 'catalog/layer_filter_attribute';
        }

        $result = $this->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();

        foreach($result->getItems() as $option) {
            echo $option->getLabel().'<br/>';
            echo $option->getValue();
        }
}

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

四叶草在未来唯美盛开 2024-09-15 04:34:01

Magento 使用模型 Catalog_Model_Layer 来完成此任务,所以我猜这可能是您最好的选择。买者自负,我还没有测试过这段代码:

$layer = Mage::getModel("catalog/layer");
foreach($categories as $categoryid) {
    $category = Mage::getModel("catalog/category")->load($categoryid);
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    // do something with your attributes
}

这里的每次迭代都会给你一个 Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute_Collection 类的对象,你应该能够在 foreach 循环中迭代它以获得所需的输出。

对于缓存,请尝试在站点上启用块缓存,并为块提供如下所示的缓存标记。 Magento 将缓存 HTML 输出,一切都会正常:

protected function _construct() {
    $this->addData(array(
        'cache_lifetime' => 3600,
        'cache_tags'     => array(Mage_Catalog_Model_Product::CACHE_TAG),
        'cache_key'      => $someUniqueIdentifierYouCreate,
    ));
}

缓存仅对您传递的密钥有效,因此请确保,如果菜单要更改(例如,不刷新缓存),缓存键不同。

希望有帮助!

谢谢,

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:

$layer = Mage::getModel("catalog/layer");
foreach($categories as $categoryid) {
    $category = Mage::getModel("catalog/category")->load($categoryid);
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();
    // do something with your attributes
}

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:

protected function _construct() {
    $this->addData(array(
        'cache_lifetime' => 3600,
        'cache_tags'     => array(Mage_Catalog_Model_Product::CACHE_TAG),
        'cache_key'      => $someUniqueIdentifierYouCreate,
    ));
}

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

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