custom/plugins/WebLa_HideWithoutStock/src/WebLa_HideWithoutStock.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WebLa_HideWithoutStock;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  7. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  14. use Shopware\Core\System\CustomField\CustomFieldTypes;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. class WebLa_HideWithoutStock extends Plugin
  19. {
  20.     const CUSTOM_FIELD_SHOW_EMPTY_STOCK 'webla_show_empty_stock';
  21.     // plugin
  22.     public function install(InstallContext $context): void
  23.     {
  24.         parent::install($context);
  25.         $prevID $this->getCustomFieldSetID($context->getContext());
  26.         $this->upsertCustomField($context->getContext(), $prevID);
  27.     }
  28.     public function update(UpdateContext $context): void
  29.     {
  30.         parent::update($context);
  31.         $prevID $this->getCustomFieldSetID($context->getContext());
  32.         $this->upsertCustomField($context->getContext(), $prevID);
  33.     }
  34.     public function activate(ActivateContext $context): void
  35.     {
  36.         parent::activate($context);
  37.     }
  38.     public function deactivate(DeactivateContext $context): void
  39.     {
  40.         parent::deactivate($context);
  41.     }
  42.     public function uninstall(UninstallContext $context): void
  43.     {
  44.         parent::uninstall($context);
  45.         if ($context->keepUserData()) {
  46.             return;
  47.         }
  48.         $this->removeConfiguration($context->getContext());
  49.         $this->removeCustomField($context);
  50.     }
  51.     // Configuration
  52.     private function removeConfiguration(Context $context): void
  53.     {
  54.         /** @var EntityRepositoryInterface $systemConfigRepository */
  55.         $systemConfigRepository $this->container->get('system_config.repository');
  56.         $criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey'$this->getName() . '.config.'));
  57.         $idSearchResult $systemConfigRepository->searchIds($criteria$context);
  58.         $ids array_map(static function ($id) {
  59.             return ['id' => $id];
  60.         }, $idSearchResult->getIds());
  61.         if ($ids === []) {
  62.             return;
  63.         }
  64.         $systemConfigRepository->delete($ids$context);
  65.     }
  66.     // custom field
  67.     private function upsertCustomField($context$id)
  68.     {
  69.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  70.         $newID $id ?? Uuid::randomHex();
  71.         $customFieldSetRepository->upsert([
  72.             [
  73.                 'id' => $newID,
  74.                 'name' => 'webla_empty_stock',
  75.                 'config' => [
  76.                     'label' => [
  77.                         'de-DE' => 'Leerer Lagerbestand',
  78.                         'en-GB' => 'Empty stock'
  79.                     ]
  80.                 ],
  81.                 'customFields' => [
  82.                     [
  83.                         'id' => $this->getCustomFieldID($contextself::CUSTOM_FIELD_SHOW_EMPTY_STOCK) ?? Uuid::randomHex(),
  84.                         'name' => self::CUSTOM_FIELD_SHOW_EMPTY_STOCK,
  85.                         'type' => CustomFieldTypes::BOOL,
  86.                         'config' => [
  87.                             'label' => [
  88.                                 'de-DE' => 'Anzeigen',
  89.                                 'en-GB' => 'Show'
  90.                             ],
  91.                             // 'type' => 'switch',
  92.                             // 'customFieldType' => 'switch',
  93.                             'customFieldPosition' => 1
  94.                         ]
  95.                     ]
  96.                 ],
  97.                 'relations' => [
  98.                     [
  99.                         'id' => $id == null Uuid::randomHex() : $this->getCustomFieldRelationID($context$id),
  100.                         'entityName' => 'product'
  101.                     ]
  102.                 ]
  103.             ]
  104.         ], $context);
  105.     }
  106.     private function removeCustomField(UninstallContext $uninstallContext)
  107.     {
  108.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  109.         $customFieldRepository $this->container->get('custom_field.repository');
  110.         $fieldSetIds $this->customFieldsExist($uninstallContext);
  111.         if ($fieldSetIds) {
  112.             $fieldSetId $this->getCustomFieldSetID($uninstallContext->getContext());
  113.             $criteria = new Criteria();
  114.             $criteria->addFilter(new EqualsFilter('customFieldSetId'$fieldSetId));
  115.             $fieldIds $customFieldRepository->searchIds($criteria$uninstallContext->getContext());
  116.             foreach($fieldIds->getData() as $id){
  117.                 $customFieldRepository->delete([['id' => $id['id']]], $uninstallContext->getContext());
  118.             }
  119.             $customFieldSetRepository->delete(array_values($fieldSetIds->getData()), $uninstallContext->getContext());
  120.         }
  121.     }
  122.     private function getCustomFieldID($context$name)
  123.     {
  124.         $customFieldRepository $this->container->get('custom_field.repository');
  125.         $criteria = new Criteria();
  126.         $criteria->addFilter(new EqualsAnyFilter('name', [$name]));
  127.         $ids $customFieldRepository->search($criteria$context)->first();
  128.         return $ids == null null $ids->getId();
  129.     }
  130.     private function getCustomFieldRelationID(Context $context$id)
  131.     {
  132.         $customFieldRepository $this->container->get('custom_field_set_relation.repository');
  133.         $criteria = new Criteria();
  134.         $criteria->addFilter(new EqualsFilter('customFieldSetId'$id));
  135.         $ids $customFieldRepository->search($criteria$context)->first();
  136.         return $ids == null null $ids->getId();
  137.     }
  138.     private function getCustomFieldSetID(Context $context)
  139.     {
  140.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  141.         $criteria = new Criteria();
  142.         $criteria->addFilter(new EqualsAnyFilter('name', [self::CUSTOM_FIELD_SHOW_EMPTY_STOCK]));
  143.         $ids $customFieldSetRepository->search($criteria$context)->first();
  144.         return $ids == null null $ids->getId();
  145.     }
  146.     private function customFieldsExist(UninstallContext $context)
  147.     {
  148.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  149.         $criteria = new Criteria();
  150.         $criteria->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SHOW_EMPTY_STOCK));
  151.         $ids $customFieldSetRepository->searchIds($criteria$context->getContext());
  152.         return $ids->getTotal() > $ids false;
  153.     }
  154. }