custom/plugins/NxsSnippetsImportExport/src/NxsSnippetsImportExport.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nxs\NxsSnippetsImportExport;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Symfony\Component\HttpFoundation\Request;
  9. if (file_exists(dirname(__DIR__) . '/packages/league/csv/autoload.php')) {
  10.     require_once dirname(__DIR__) . '/packages/league/csv/autoload.php';
  11. }
  12. class NxsSnippetsImportExport extends Plugin
  13. {
  14.     public function install(InstallContext $installContext): void
  15.     {
  16.         parent::install($installContext);
  17.         //bereits überarbeitet
  18.         //$this->importShopwareSnippets($installContext);
  19.     }
  20.     private function importShopwareSnippets(InstallContext $installContext)
  21.     {
  22.         $snippetSet $this->container->get('snippet_set.repository')->search((new Criteria())->addFilter(new EqualsFilter('name''BASE de-DE'))->addAssociation('snippets'), $installContext->getContext());
  23.         $request = new Request();
  24.         $snippetController $this->container->get('Shopware\Core\System\Snippet\Api\SnippetController');
  25.         $total = \GuzzleHttp\json_decode($snippetController->getList($request$installContext->getContext())->getContent(),true)['total'];
  26.         if (count($snippetSet->first()->getSnippets()) < $total){
  27.             $request->request->set('limit'$total);
  28.             $snippets = \GuzzleHttp\json_decode($snippetController->getList($request$installContext->getContext())->getContent(),true);
  29.             /** @var EntityRepositoryInterface $snippetsRepository */
  30.             $snippetsRepository $this->container->get('snippet.repository');
  31.             foreach ($snippets['data'] as $snippet){
  32.                 try{
  33.                     if (empty($snippet[1]['author'])){
  34.                         unset($snippet[1]);
  35.                     }
  36.                     $snippetsRepository->upsert($snippet$installContext->getContext());
  37.                 }catch (\Exception $e){
  38.                 }
  39.             }
  40.         }
  41.     }
  42. }