BieneMaja
BieneMaja

Reputation: 21

Shopware6 Plugin Criteria all 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:

<?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]);
    }

}

And 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>

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.

Upvotes: 2

Views: 174

Answers (2)

wazka83
wazka83

Reputation: 11

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

$event->getSalesChannelContext()

And you can get Context by

$event->getSalesChannelContext()->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.

Upvotes: 1

Alex
Alex

Reputation: 35008

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();

Upvotes: 2

Related Questions