vendor/store.shopware.com/stuttnewsletterpromotion/src/StuttNewsletterPromotion.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Stutt\NewsletterPromotion;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  4. use Shopware\Core\Framework\Uuid\Uuid;
  5. use Shopware\Core\System\CustomField\CustomFieldEntity;
  6. use Shopware\Core\System\CustomField\CustomFieldTypes;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. class StuttNewsletterPromotion extends Plugin
  12. {
  13.     public function update(\Shopware\Core\Framework\Plugin\Context\UpdateContext $updateContext): void
  14.     {
  15.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  16.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  17.         $criteria = new Criteria();
  18.         $criteria->addFilter(
  19.             new EqualsFilter('name''stutt_newsletter')
  20.         );
  21.         $criteria->addAssociation('customFields');
  22.         $sets $customFieldSetRepository->search($criteria$updateContext->getContext());
  23.         if ($sets->count() === 0) {
  24.             $this->addCustomFields($updateContext->getContext());
  25.         }
  26.         else {
  27.             /** @var \Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity $set */
  28.             $set $sets->first();
  29.             $customFields $set->getCustomFields();
  30.             $found FALSE;
  31.             foreach ($customFields->getIterator() as $customField) {
  32.                 if ($customField->getName() === 'stutt_voucher_perm') {
  33.                     $found TRUE;
  34.                 }
  35.             }
  36.             if (!$found) {
  37.                 /** @var EntityRepositoryInterface $customFieldRepository */
  38.                 $customFieldRepository $this->container->get('custom_field.repository');
  39.                 $criteria = new Criteria();
  40.                 $criteria->addFilter(
  41.                     new EqualsFilter('name''stutt_voucher_perm')
  42.                 );
  43.                 $idDel $customFieldRepository->searchIds($criteria$updateContext->getContext())->firstId();
  44.                 $customFieldRepository->delete([
  45.                     [
  46.                         'id' => $idDel
  47.                     ]
  48.                 ], $updateContext->getContext());
  49.                 $id Uuid::randomHex();
  50.                 $result $customFieldRepository->create([[
  51.                     'id' => $id,
  52.                     'name' => 'stutt_voucher_perm',
  53.                     'type' => CustomFieldTypes::TEXT
  54.                 ]], $updateContext->getContext());
  55.                 $customFieldSetRepository->update([
  56.                     [
  57.                         'id' => $set->getId(),
  58.                         'customFields' => [
  59.                             [
  60.                                 'id' => $id
  61.                             ]
  62.                         ]
  63.                     ]
  64.                 ], $updateContext->getContext());
  65.             }
  66.         }
  67.         parent::update($updateContext);
  68.     }
  69.     public function install(InstallContext $installContext): void
  70.     {
  71.         parent::install($installContext);
  72.         $this->addCustomFields($installContext->getContext());
  73.     }
  74.     private function addCustomFields(\Shopware\Core\Framework\Context $context)
  75.     {
  76.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  77.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  78.         $criteria = new Criteria();
  79.         $criteria->addFilter(
  80.             new EqualsFilter('name''stutt_newsletter')
  81.         );
  82.         $sets $customFieldSetRepository->search($criteria$context);
  83.         if (!$sets->count()) {
  84.             $customFieldSetRepository->create([[
  85.                 'name' => 'stutt_newsletter',
  86.                 'customFields' => [
  87.                     [
  88.                         'name' => 'stutt_voucher',
  89.                         'type' => CustomFieldTypes::TEXT
  90.                     ],
  91.                     [
  92.                         'name' => 'stutt_voucher_perm',
  93.                         'type' => CustomFieldTypes::TEXT
  94.                     ]
  95.                 ],
  96.                 'relations' => [
  97.                     [
  98.                         'entityName' => 'newsletter_recipient'
  99.                     ]
  100.                 ],
  101.             ]], $context);
  102.         }
  103.     }
  104. }