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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CustomerGroupAvailableProduct;
  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\EntitySearchResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. use Shopware\Core\System\Snippet\SnippetEntity;
  15. class AcrisCustomerGroupAvailableProductCS extends Plugin
  16. {
  17.     const CUSTOM_FIELD_SET_NAME_CUSTOMER_GROUP_PRODUCT 'acris_customer_group_available_product';
  18.     const CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP 'acris_customer_group_available_product_exclude_sitemap';
  19.     public function uninstall(UninstallContext $context): void
  20.     {
  21.         parent::uninstall($context);
  22.         if ($context->keepUserData()) {
  23.             return;
  24.         }
  25.         $connection $this->container->get(Connection::class);
  26.         $this->removeTableAndFields($connection);
  27.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_CUSTOMER_GROUP_PRODUCT]);
  28.     }
  29.     public function install(InstallContext $context): void
  30.     {
  31.         $this->addCustomFields($context->getContext());
  32.     }
  33.     public function update(UpdateContext $context): void
  34.     {
  35.         $this->addCustomFields($context->getContext());
  36.     }
  37.     public function removeTableAndFields(Connection $connection)
  38.     {
  39.         $connection->executeStatement('DROP TABLE IF EXISTS `acris_customer_group_available_product`');
  40.         $connection->executeStatement('ALTER TABLE `product` DROP COLUMN `acrisReleaseCustomerGroup`');
  41.         $connection->executeStatement('ALTER TABLE `customer_group` DROP COLUMN `product`');
  42.         try {
  43.             $connection->executeStatement('ALTER TABLE `product` DROP COLUMN `customerGroup`');
  44.         } catch (\Throwable $e) {}
  45.     }
  46.     private function addCustomFields(Context $context): void
  47.     {
  48.         /* Check for snippets if they exist for custom fields */
  49.         $this->checkForExistingCustomFieldSnippets($context);
  50.         $customFieldSet $this->container->get('custom_field_set.repository');
  51.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_CUSTOMER_GROUP_PRODUCT)), $context)->count() == 0) {
  52.             $customFieldSet->create([[
  53.                 'name' => self::CUSTOM_FIELD_SET_NAME_CUSTOMER_GROUP_PRODUCT,
  54.                 'config' => [
  55.                     'label' => [
  56.                         'en-GB' => 'Customer group available products',
  57.                         'de-DE' => 'Kundengruppe verfügbare Produkte'
  58.                     ]
  59.                 ],
  60.                 'customFields' => [
  61.                     ['name' => self::CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP'type' => CustomFieldTypes::BOOL,
  62.                         'config' => [
  63.                             'componentName' => 'sw-field',
  64.                             'type' => 'checkbox',
  65.                             'customFieldType' => 'checkbox',
  66.                             'customFieldPosition' => 1,
  67.                             'label' => [
  68.                                 'en-GB' => 'Exclude from sitemap',
  69.                                 'de-DE' => 'Von Sitemap ausnehmen'
  70.                             ]
  71.                         ]]
  72.                 ],
  73.             ]], $context);
  74.         }
  75.     }
  76.     private function removeCustomFields(Context $context, array $setNames): void
  77.     {
  78.         /* Check for snippets if they exist for custom fields */
  79.         $this->checkForExistingCustomFieldSnippets($context);
  80.         $customFieldSet $this->container->get('custom_field_set.repository');
  81.         foreach ($setNames as $setName) {
  82.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  83.             if($id$customFieldSet->delete([['id' => $id]], $context);
  84.         }
  85.     }
  86.     private function checkForExistingCustomFieldSnippets(Context $context)
  87.     {
  88.         /** @var EntityRepositoryInterface $snippetRepository */
  89.         $snippetRepository $this->container->get('snippet.repository');
  90.         $criteria = new Criteria();
  91.         $criteria->addFilter(new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP));
  92.         /** @var EntitySearchResult $searchResult */
  93.         $searchResult $snippetRepository->search($criteria$context);
  94.         if ($searchResult->count() > 0) {
  95.             $snippetIds = [];
  96.             /** @var SnippetEntity $snippet */
  97.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  98.                 $snippetIds[] = [
  99.                     'id' => $snippet->getId()
  100.                 ];
  101.             }
  102.             if (!empty($snippetIds)) {
  103.                 $snippetRepository->delete($snippetIds$context);
  104.             }
  105.         }
  106.     }
  107. }