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

Shopware6插件获取Sales Channel国家数据遇参数类型不匹配错误

Shopware 6插件获取国家数据报错问题

问题描述

开发Shopware 6插件时,需要从销售渠道读取所有国家数据并传递至前端,前端传值逻辑已验证可行,但数据获取环节出现异常。

PHP代码

<?php declare(strict_types=1);

namespace MyWave\Service;

use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;


class AddDataToPage implements EventSubscriberInterface
{
    
    private EntityRepositoryInterface $countryRepository;
    
    public function __construct(
        EntityRepositoryInterface $countryRepository
    )
    {
        $this->countryRepository = $countryRepository;
    }

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

    public function addAllCountries(FooterPageletLoadedEvent $event, SalesChannelContext $context): void
    {
        //Placeholder
        $countries = "countries";
        $criteria = new Criteria();
        $country_results = $this->countryRepository->search($criteria, $context->getContext())->getEntities();
        $country_results_encode = json_encode($country_results, true);
        $country_results_decode = json_decode($country_results_encode, true);
        $event->getPagelet()->assign( ['allcountries' => $country_results_decode]);
    }

}

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="MyWave\Service\AddDataToPage" >
            <argument type="service" id="country.repository"/>
            <tag name="kernel.event_subscriber" />
        </service>
    </services>
</container>

错误信息

MyWave\Service\AddDataToPage::addAllCountries():
Argument #2 ($context) must be of type Shopware\Core\System\SalesChannel\SalesChannelContext,
string given, called in /var/www/clients/client1/web2/web/projekte/shopware-demo/
vendor/symfony/event-dispatcher/Debug/WrappedListener.php on line 117

测试用字符串"countries"传递时功能正常,预期能成功获取所有国家数据并传递至前端。

解决方案

1. 错误原因

Symfony事件订阅者的方法默认仅接收事件对象作为第一个参数。你在addAllCountries方法中声明了第二个参数SalesChannelContext $context,但Symfony会将**事件名称(字符串)**传入第二个参数位置,导致类型不匹配报错。

2. 修复步骤

从FooterPageletLoadedEvent事件对象中直接获取SalesChannelContext,该事件本身已经包含了上下文信息,无需在方法参数中额外声明。

修改后的addAllCountries方法代码:

public function addAllCountries(FooterPageletLoadedEvent $event): void
{
    // 从事件对象中获取SalesChannelContext
    $context = $event->getSalesChannelContext();
    
    $criteria = new Criteria();
    $country_results = $this->countryRepository->search($criteria, $context->getContext())->getEntities();
    
    // 无需JSON编码解码,直接用实体集合的toArray()方法转数组
    $event->getPagelet()->assign(['allcountries' => $country_results->toArray()]);
}

3. 额外优化

  • 去掉冗余的json_encode+json_decode操作:Shopware的实体集合自带toArray()方法,能直接将实体数据转为数组,效率更高。
  • 若需过滤销售渠道可用的国家,可在Criteria中添加过滤条件,比如$criteria->addFilter(new EqualsFilter('active', true));如果需要所有国家则保持当前空Criteria即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 23:27:45