Shopware6插件条件所有国家

huangapple go评论56阅读模式
英文:

Shopware6 Plugin Criteria all Countries

问题

My Shopware 6 plugin 应该读取所有 Sales Channel 的国家,然后将它们传递到前端。这些值已经成功传递到前端,然而数据检索不起作用。以下是文件:

<?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
    {
        //占位符
        $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" 时可以正常工作。

英文:

My Shopware 6 plugin is supposed to read all countries from the Sales Channel and then pass them on to the frontend. The values are successfully passed on to the frontend, however, the data retrieval is not working. Here are the files:

&lt;?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-&gt;countryRepository = $countryRepository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            FooterPageletLoadedEvent::class =&gt; &#39;addAllCountries&#39;
        ];
    }

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

}

And xml:

&lt;?xml version=&quot;1.0&quot; ?&gt;

&lt;container xmlns=&quot;http://symfony.com/schema/dic/services&quot;
           xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xsi:schemaLocation=&quot;http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd&quot;&gt;

    &lt;services&gt;
        &lt;service id=&quot;MyWave\Service\AddDataToPage&quot; &gt;
            &lt;argument type=&quot;service&quot; id=&quot;country.repository&quot;/&gt;
            &lt;tag name=&quot;kernel.event_subscriber&quot; /&gt;
        &lt;/service&gt;
    &lt;/services&gt;
&lt;/container&gt;

The following error occurs:

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

Where is my mistake, can someone help me?

As described above, I expected to retrieve all countries and pass them on to the frontend with the variable. With the demo string: countries, it worked.

答案1

得分: 2

以下是您要翻译的内容:

The event is dispatched like this in the core:

$this->eventDispatcher->dispatch(
    new FooterPageletLoadedEvent($pagelet, $salesChannelContext, $request)
);

But this does not mean, that the listener is called with the same parameters.

The salesChannelContext is contained in the event object and must be fetched from there using getSalesChannelContext:

public function addAllCountries(FooterPageletLoadedEvent $event): void
{
    $context = $event->getSalesChannelContext();

You also get the context directly:

$country_results = $this->countryRepository->search($criteria, $event->getContext())->getEntities();
英文:

The event is dispatched like this in the core:

$this-&gt;eventDispatcher-&gt;dispatch(
    new FooterPageletLoadedEvent($pagelet, $salesChannelContext, $request)
);

But this does not mean, that the listener is called with the same parameters.

The salesChannelContext is contained in the event object and must be fetched from there using getSalesChannelContext:

public function addAllCountries(FooterPageletLoadedEvent $event): void
{
    $context = $event-&gt;getSalesChannelContext();

You also get the context directly:

$country_results = $this-&gt;countryRepository-&gt;search($criteria, $event-&gt;getContext())-&gt;getEntities();

答案2

得分: 1

首先,只有事件对象被传递给监听方法。您可以通过以下方式获取SalesChannelContext

$event->getSalesChannelContext()

然后,您可以获取Context

$event->getSalesChannelContext()->getContext()

其次,在从存储库获取实体时,为什么要对数据进行编码然后解码呢?这些数据是实体的集合。集合有一个toArray()方法或类似的方法,您可以直接使用它。

英文:

Firstly only event object is passed to listener method. You can get SalesChannelContext by

$event-&gt;getSalesChannelContext()

And you can get Context by

$event-&gt;getSalesChannelContext()-&gt;getContext()

Secondly, when fetching entities from repository why do u encode and then decode the data? The data is collection of entities. Collection has method toArray() or sth like that. You can just use it.

huangapple
  • 本文由 发表于 2023年2月24日 06:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定