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

如何将Shopware 6主题的Config变量传递到店铺前端?

Shopware 6主题实现config.xml变量传递到前端的方法

要实现将主题config.xml中的配置变量传递到店铺前端Twig模板,按以下步骤操作:

1. 定义主题配置(config.xml)

在你的主题目录src/Resources/config/下创建或修改config.xml,添加需要的配置字段。示例:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/trunk/src/Core/System/Theme/Xml/theme-1.0.xsd">
    <fields>
        <card>
            <title>自定义配置</title>
            <elements>
                <text-field>
                    <name>customTitle</name>
                    <label>页面自定义标题</label>
                    <default-value>我的店铺</default-value>
                </text-field>
                <color-picker>
                    <name>primaryColor</name>
                    <label>主色调</label>
                    <default-value>#ff0000</default-value>
                </color-picker>
            </elements>
        </card>
    </fields>
</config>

2. 创建事件订阅器传递配置到Twig

在主题的src/Subscriber/目录下创建ThemeConfigSubscriber.php,通过StorefrontRenderEvent将主题配置注入Twig上下文:

<?php

namespace YourThemeName\Subscriber;

use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ThemeConfigSubscriber implements EventSubscriberInterface
{
    private SystemConfigService $systemConfigService;

    public function __construct(SystemConfigService $systemConfigService)
    {
        $this->systemConfigService = $systemConfigService;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            StorefrontRenderEvent::class => 'onStorefrontRender'
        ];
    }

    public function onStorefrontRender(StorefrontRenderEvent $event): void
    {
        // 获取当前主题的所有配置
        $themeConfig = $this->systemConfigService->get('YourThemeName.config', $event->getSalesChannelContext()->getSalesChannelId());
        
        // 将配置注入Twig变量(对应截图中的ssik变量)
        $event->getSalesChannelContext()->assign([
            'ssik' => $themeConfig
        ]);
    }
}

注意:将YourThemeName替换为你的主题实际名称。

3. 注册订阅器

在主题的src/Resources/config/services.xml中注册上述订阅器:

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="YourThemeName\Subscriber\ThemeConfigSubscriber">
            <argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
            <tag name="kernel.event_subscriber"/>
        </service>
    </services>
</container>

4. 在Twig模板中使用变量

完成上述步骤后,即可在任意Storefront Twig模板中直接调用变量,比如:

{# 输出自定义标题 #}
<h1>{{ ssik.customTitle }}</h1>

{# 输出主色调 #}
<style>
    .primary-color { color: {{ ssik.primaryColor }}; }
</style>

{# 查看所有配置变量 #}
{{ dump(ssik) }}

内容的提问来源于stack exchange,提问作者Templator Khan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 05:07:20