custom/plugins/TonurGiftWrap/src/TonurGiftWrap.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tonur\GiftOption;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Shopware\Core\System\CustomField\CustomFieldTypes;
  13. class TonurGiftWrap extends Plugin
  14. {
  15.     public const BUNDLE_NAME 'TonurGiftWrap';
  16.     public const CUSTOMFIELD_SET_GIFT_WRAP_PRODUCT "repertus_gift_wrap_product";
  17.     public const CUSTOMFIELD_GIFT_WRAP_EXCLUDE_PRODUCT "repertus_gift_wrap_exclude_product";
  18.     /** @var EntityRepositoryInterface $customFieldSetRepository */
  19.     private $customFieldSetRepository;
  20.     public function install(InstallContext $installContext): void
  21.     {
  22.         parent::install($installContext);
  23.         $this->addCustomFieldSet($installContext->getContext());
  24.     }
  25.     public function update(UpdateContext $updateContext): void
  26.     {
  27.         parent::update($updateContext);
  28.         $this->addCustomFieldSet($updateContext->getContext());
  29.     }
  30.     public function uninstall(UninstallContext $uninstallContext): void
  31.     {
  32.         parent::uninstall($uninstallContext);
  33.         if ($uninstallContext->keepUserData()) {
  34.             return;
  35.         }
  36.         $this->removeCustomFieldSet($uninstallContext->getContext());
  37.         $connection $this->container->get(Connection::class);
  38.         $connection->executeStatement('DROP TABLE IF EXISTS `repertus_gift_wrap`');
  39.     }
  40.     private function addCustomFieldSet(Context $context)
  41.     {
  42.         $criteria = new Criteria();
  43.         $criteria
  44.             ->addAssociation('customFields')
  45.             ->addFilter(new EqualsFilter('customFields.name'self::CUSTOMFIELD_GIFT_WRAP_EXCLUDE_PRODUCT));
  46.         $searchResult $this->getCustomFieldSetRepository()->search($criteria$context);
  47.         if ($searchResult->count() == 0) {
  48.             $this->getCustomFieldSetRepository()->create([
  49.                 [
  50.                     'name' => self::CUSTOMFIELD_SET_GIFT_WRAP_PRODUCT,
  51.                     'config' => [
  52.                         'label' => [
  53.                             'de-DE' => 'Geschenkverpackung',
  54.                             'en-GB' => 'Gift Wrap'
  55.                         ]
  56.                     ],
  57.                     'relations' => [[
  58.                         'entityName' => 'product'
  59.                     ]],
  60.                     'customFields' => [
  61.                         [
  62.                             'name' => self::CUSTOMFIELD_GIFT_WRAP_EXCLUDE_PRODUCT,
  63.                             'type' => CustomFieldTypes::BOOL,
  64.                             'config' => [
  65.                                 'type' => CustomFieldTypes::SWITCH,
  66.                                 'label' => [
  67.                                     "de-DE" => "Produkt nicht als Geschenk verpacken",
  68.                                     "en-GB" => "Do not gift wrap the product"
  69.                                 ],
  70.                                 "componentName" => "sw-field",
  71.                                 "customFieldType" => CustomFieldTypes::SWITCH,
  72.                                 "customFieldPosition" => 1
  73.                             ]
  74.                         ]
  75.                     ]
  76.                 ]
  77.             ], $context);
  78.         }
  79.     }
  80.     private function removeCustomFieldSet(Context $context)
  81.     {
  82.         $criteria = new Criteria();
  83.         $criteria->addFilter(new EqualsFilter('name'self::CUSTOMFIELD_SET_GIFT_WRAP_PRODUCT));
  84.         $idSearchResult $this->getCustomFieldSetRepository()->searchIds($criteria$context);
  85.         $ids \array_map(static function ($id) {
  86.             return ['id' => $id];
  87.         }, $idSearchResult->getIds());
  88.         $this->getCustomFieldSetRepository()->delete($ids$context);
  89.     }
  90.     private function getCustomFieldSetRepository(): EntityRepositoryInterface
  91.     {
  92.         if ($this->customFieldSetRepository === null) {
  93.             $this->customFieldSetRepository $this->container->get('custom_field_set.repository');
  94.         }
  95.         return $this->customFieldSetRepository;
  96.     }
  97. }