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->executeUpdate('DROP TABLE IF EXISTS `acris_customer_group_available_product`');
  40.         $connection->executeUpdate('ALTER TABLE `product` DROP COLUMN `customerGroup`');
  41.         $connection->executeUpdate('ALTER TABLE `customer_group` DROP COLUMN `product`');
  42.     }
  43.     private function addCustomFields(Context $context): void
  44.     {
  45.         /* Check for snippets if they exist for custom fields */
  46.         $this->checkForExistingCustomFieldSnippets($context);
  47.         $customFieldSet $this->container->get('custom_field_set.repository');
  48.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_CUSTOMER_GROUP_PRODUCT)), $context)->count() == 0) {
  49.             $customFieldSet->create([[
  50.                 'name' => self::CUSTOM_FIELD_SET_NAME_CUSTOMER_GROUP_PRODUCT,
  51.                 'config' => [
  52.                     'label' => [
  53.                         'en-GB' => 'Customer group available products',
  54.                         'de-DE' => 'Kundengruppe verfügbare Produkte'
  55.                     ]
  56.                 ],
  57.                 'customFields' => [
  58.                     ['name' => self::CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP'type' => CustomFieldTypes::BOOL,
  59.                         'config' => [
  60.                             'componentName' => 'sw-field',
  61.                             'type' => 'checkbox',
  62.                             'customFieldType' => 'checkbox',
  63.                             'customFieldPosition' => 1,
  64.                             'label' => [
  65.                                 'en-GB' => 'Exclude from sitemap',
  66.                                 'de-DE' => 'Von Sitemap ausnehmen'
  67.                             ]
  68.                         ]]
  69.                 ],
  70.             ]], $context);
  71.         }
  72.     }
  73.     private function removeCustomFields(Context $context, array $setNames): void
  74.     {
  75.         /* Check for snippets if they exist for custom fields */
  76.         $this->checkForExistingCustomFieldSnippets($context);
  77.         $customFieldSet $this->container->get('custom_field_set.repository');
  78.         foreach ($setNames as $setName) {
  79.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  80.             if($id$customFieldSet->delete([['id' => $id]], $context);
  81.         }
  82.     }
  83.     private function checkForExistingCustomFieldSnippets(Context $context)
  84.     {
  85.         /** @var EntityRepositoryInterface $snippetRepository */
  86.         $snippetRepository $this->container->get('snippet.repository');
  87.         $criteria = new Criteria();
  88.         $criteria->addFilter(new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP));
  89.         /** @var EntitySearchResult $searchResult */
  90.         $searchResult $snippetRepository->search($criteria$context);
  91.         if ($searchResult->count() > 0) {
  92.             $snippetIds = [];
  93.             /** @var SnippetEntity $snippet */
  94.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  95.                 $snippetIds[] = [
  96.                     'id' => $snippet->getId()
  97.                 ];
  98.             }
  99.             if (!empty($snippetIds)) {
  100.                 $snippetRepository->delete($snippetIds$context);
  101.             }
  102.         }
  103.     }
  104. }