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

Shopware 6.1自定义插件配置详情获取方法(6.5可用config()函数)

在Shopware 6.1前端获取自定义插件配置的方法

Twig 文件中获取配置

Shopware 6.1没有内置的config()函数,需要通过事件订阅将配置注入到页面上下文,再在Twig中调用:

  1. 创建页面加载事件订阅者,读取插件配置并添加到页面扩展
// src/Subscriber/StorefrontPageSubscriber.php
<?php

namespace YourPluginName\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class StorefrontPageSubscriber implements EventSubscriberInterface
{
    private $systemConfigRepository;

    public function __construct(EntityRepository $systemConfigRepository)
    {
        $this->systemConfigRepository = $systemConfigRepository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            PageLoadedEvent::class => 'onPageLoaded',
        ];
    }

    public function onPageLoaded(PageLoadedEvent $event): void
    {
        $context = $event->getContext();
        // 替换为你的插件配置前缀,格式:插件名.config
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('configurationKey', 'YourPluginName.config'));
        $config = $this->systemConfigRepository->search($criteria, $context)->first();

        if ($config) {
            $event->getPage()->addExtension('your_plugin_config', $config->getConfigurationValue());
        }
    }
}
  1. 在services.xml中注册订阅者
<!-- src/Resources/config/services.xml -->
<service id="YourPluginName\Subscriber\StorefrontPageSubscriber">
    <argument type="service" id="system_config.repository"/>
    <tag name="kernel.event_subscriber"/>
</service>
  1. 在Twig模板中调用配置
{# 示例:读取配置中的某个字段 #}
{{ page.extensions.your_plugin_config.yourConfigKey }}

JS 文件中获取配置

有两种常用方式:

方式一:通过Twig注入全局变量

在你的模板文件(如插件自定义模板或base.html.twig)中添加以下代码,将配置转为全局JS变量:

<script>
    window.yourPluginConfig = {{ page.extensions.your_plugin_config|json_encode|raw }};
</script>

之后在JS文件中直接访问:

console.log(window.yourPluginConfig.yourConfigKey);
// 基于配置执行逻辑

方式二:通过自定义API接口获取

  1. 创建Storefront控制器提供配置接口
// src/Controller/ConfigController.php
<?php

namespace YourPluginName\Controller;

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

#[Route(defaults: ['_routeScope' => ['storefront']])]
class ConfigController extends AbstractController
{
    private $systemConfigRepository;

    public function __construct(EntityRepository $systemConfigRepository)
    {
        $this->systemConfigRepository = $systemConfigRepository;
    }

    #[Route(path: '/your-plugin/config', name: 'frontend.your_plugin.config', methods: ['GET'])]
    public function getConfig(): JsonResponse
    {
        $context = $this->get('shopware.storefront.context_service')->getShopContext();
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('configurationKey', 'YourPluginName.config'));
        $config = $this->systemConfigRepository->search($criteria, $context)->first();

        return new JsonResponse($config ? $config->getConfigurationValue() : []);
    }
}
  1. 在JS中请求接口获取配置
fetch('/your-plugin/config')
    .then(response => response.json())
    .then(config => {
        console.log(config.yourConfigKey);
        // 后续业务逻辑
    })
    .catch(error => console.error('获取配置失败:', error));

内容的提问来源于stack exchange,提问作者hari prasath

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 16:00:38