You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware6自定义区块中如何获取当前分类的子分类列表

在Shopware 6.4列表页CMS区块中获取当前分类及子分类

实现方案

要在自定义CMS区块中展示当前分类的子分类,不能仅依赖模板dump(),需通过扩展CMS区块的数据源来主动注入分类数据,具体步骤如下:

1. 创建CMS区块数据处理器

编写一个继承AbstractCmsElementResolver的处理器类,负责获取当前分类并查询其子分类:

<?php

namespace YourPlugin\Core\Content\Cms\Resolver;

use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;

class CategoryListElementResolver extends AbstractCmsElementResolver
{
    private $categoryRepository;

    public function __construct(\Shopware\Core\Content\Category\CategoryRepository $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }

    public function getType(): string
    {
        return 'your-custom-category-block'; // 替换为你的区块类型标识
    }

    public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?ElementDataCollection
    {
        // 从请求中获取当前分类ID
        $categoryId = $resolverContext->getRequest()->get('navigationId');
        if (!$categoryId) {
            return null;
        }

        // 查询当前分类及关联子分类
        $criteria = new Criteria([$categoryId]);
        $criteria->addAssociation('children');
        $category = $this->categoryRepository->search($criteria, $resolverContext->getContext())->first();

        if (!$category) {
            return null;
        }

        // 将数据存入集合
        $data = new ElementDataCollection();
        $data->set('currentCategory', $category);
        $data->set('childCategories', $category->getChildren());

        return $data;
    }

    public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
    {
        $slot->setData($result->all());
    }
}

2. 注册处理器到DI容器

在插件的services.xml中添加配置,让Shopware识别该处理器:

<service id="YourPlugin\Core\Content\Cms\Resolver\CategoryListElementResolver">
    <argument type="service" id="category.repository"/>
    <tag name="shopware.cms.data_resolver"/>
</service>

3. 在模板中使用注入的数据

现在你的CMS区块模板可以直接调用注入的变量,渲染子分类列表:

{% if currentCategory %}
    <h3>{{ currentCategory.name }}</h3>
    {% if childCategories is not empty %}
        <ul class="category-children">
            {% for category in childCategories %}
                <li>
                    <a href="{{ path('frontend.navigation.page', { navigationId: category.id }) }}">
                        {{ category.name }}
                    </a>
                </li>
            {% endfor %}
        </ul>
    {% endif %}
{% endif %}

关键说明

  • 列表页CMS区块默认数据源不包含分类数据,必须通过自定义处理器主动注入
  • navigationId是Shopware列表页请求中携带的分类ID参数,通过ResolverContext获取最可靠
  • 查询分类时需添加children关联,才能直接获取子分类数据

内容的提问来源于stack exchange,提问作者Mirko Benini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 14:52:43