<?php declare(strict_types=1);
namespace Nxs\NxsSnippetsImportExport;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Symfony\Component\HttpFoundation\Request;
if (file_exists(dirname(__DIR__) . '/packages/league/csv/autoload.php')) {
require_once dirname(__DIR__) . '/packages/league/csv/autoload.php';
}
class NxsSnippetsImportExport extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
//bereits überarbeitet
//$this->importShopwareSnippets($installContext);
}
private function importShopwareSnippets(InstallContext $installContext)
{
$snippetSet = $this->container->get('snippet_set.repository')->search((new Criteria())->addFilter(new EqualsFilter('name', 'BASE de-DE'))->addAssociation('snippets'), $installContext->getContext());
$request = new Request();
$snippetController = $this->container->get('Shopware\Core\System\Snippet\Api\SnippetController');
$total = \GuzzleHttp\json_decode($snippetController->getList($request, $installContext->getContext())->getContent(),true)['total'];
if (count($snippetSet->first()->getSnippets()) < $total){
$request->request->set('limit', $total);
$snippets = \GuzzleHttp\json_decode($snippetController->getList($request, $installContext->getContext())->getContent(),true);
/** @var EntityRepositoryInterface $snippetsRepository */
$snippetsRepository = $this->container->get('snippet.repository');
foreach ($snippets['data'] as $snippet){
try{
if (empty($snippet[1]['author'])){
unset($snippet[1]);
}
$snippetsRepository->upsert($snippet, $installContext->getContext());
}catch (\Exception $e){
}
}
}
}
}