custom/plugins/EwGrecoBundle/src/EwGrecoBundle.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ew\GrecoBundle;
  3. use Doctrine\DBAL\Exception;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Shopware\Core\System\CustomField\CustomFieldTypes;
  10. class EwGrecoBundle extends Plugin
  11. {
  12.     protected const BUNDLE_ARTICLE 'greco_bundle_article';
  13.     protected const DNA_ARTICLE 'greco_dna_article';
  14.     protected const PSEUDO_ARTICLE 'greco_pseudo_article';
  15.     protected const PSEUDO_ARTICLES 'greco_pseudo_articles';
  16.     /**
  17.      * @param InstallContext $context
  18.      * @throws Exception
  19.      */
  20.     public function install(InstallContext $context) : void
  21.     {
  22.         parent::install($context);
  23.         $customerFieldsRepository $this->container->get('custom_field_set.repository');
  24.         $this->addCustomFieldsToCustomer($context$customerFieldsRepository);
  25.     }
  26.     /**
  27.      * @return array[]
  28.      */
  29.     private function getCustomFields(): array
  30.     {
  31.         return [
  32.             [
  33.                 'name' => self::BUNDLE_ARTICLE,
  34.                 'type' => CustomFieldTypes::SWITCH,
  35.                 'config' => [
  36.                     'label' => [
  37.                         'de-DE' => 'Bundle Rabatt',
  38.                         'en-GB' => 'Bundle Article'
  39.                     ],
  40.                     'customFieldPosition' => 1
  41.                 ]
  42.             ],
  43.             [
  44.                 'name' => self::DNA_ARTICLE,
  45.                 'type' => CustomFieldTypes::SWITCH,
  46.                 'config' => [
  47.                     'label' => [
  48.                         'de-DE' => 'DNA Produkt',
  49.                         'en-GB' => 'DNA Article'
  50.                     ],
  51.                     'helpText' => [
  52.                         'de-DE' => 'Dann nur einmal pro Benutzer(Konto) bestellt werden',
  53.                         'en-GB' => 'Can only be ordered once per user(account)',
  54.                     ],
  55.                     'customFieldPosition' => 2
  56.                 ]
  57.             ],
  58.             [
  59.                 'name' => self::PSEUDO_ARTICLE,
  60.                 'type' => CustomFieldTypes::SWITCH,
  61.                 'config' => [
  62.                     'label' => [
  63.                         'de-DE' => 'Pseudo-Artikel',
  64.                         'en-GB' => 'Pseudo Article'
  65.                     ],
  66.                     'helpText' => [
  67.                         'de-DE' => 'When this product is added to cart it will add products that are selected in dropdown instead of this one.',
  68.                         'en-GB' => 'Wenn dieses Produkt in den Warenkorb gelegt wird, werden anstelle dieses Produkts Produkte hinzugefügt, die in der Dropdown-Liste ausgewählt wurden.',
  69.                     ],
  70.                     'customFieldPosition' => 3
  71.                 ]
  72.             ],
  73.             [
  74.                 'name' => self::PSEUDO_ARTICLES,
  75.                 'type' => CustomFieldTypes::SELECT,
  76.                 'config' => [
  77.                     'componentName' => 'sw-entity-multi-id-select',
  78.                     'customFieldType' => 'entity',
  79.                     'entity' => 'product',
  80.                     'label' => [
  81.                         'de-DE' => 'Pseudo-Produkte',
  82.                         'en-GB' => 'Pseudo Products'
  83.                     ],
  84.                     'helpText' => [
  85.                         'de-DE' => 'Choose products that will replace pseudo article.',
  86.                         'en-GB' => 'Wählen Sie Produkte, die Pseudoartikel ersetzen.',
  87.                     ],
  88.                     'customFieldPosition' => 4
  89.                 ]
  90.             ]
  91.         ];
  92.     }
  93.     /**
  94.      * @throws Exception
  95.      */
  96.     private function addCustomFieldsToCustomer($context, ?object $customFieldSetRepository): void
  97.     {
  98.         $this->uninstallCustomFieldset($context->getContext());
  99.         $customFieldSetRepository->create([[
  100.             'name' => 'ew-greco-bundle',
  101.             'position' => 0,
  102.             'global' => true,
  103.             'active' => true,
  104.             'config' => [
  105.                 'label' => [
  106.                     'en-GB' => 'EW Bundle Fields',
  107.                     'de-DE' => 'Bundle-Rabatt-Produkt'
  108.                 ],
  109.             ],
  110.             'customFields' => $this->getCustomFields(),
  111.             'relations' => [
  112.                 [
  113.                     'entityName' => 'product'
  114.                 ]
  115.             ]
  116.         ]], $context->getContext());
  117.     }
  118.     public function uninstall(UninstallContext $uninstallContext): void
  119.     {
  120.         if ($uninstallContext->keepUserData()) {
  121.             parent::uninstall($uninstallContext);
  122.             return;
  123.         }
  124.         $this->uninstallCustomFieldset($uninstallContext->getContext());
  125.     }
  126.     private function uninstallCustomFieldset($context){
  127.         $cfsRepo $this->container->get('custom_field_set.repository');
  128.         $cfRepo $this->container->get('custom_field.repository');
  129.         //delete custom_field_set entry
  130.         $cfsId $cfsRepo->search((new Criteria())
  131.             ->addFilter(new EqualsFilter('name''ew-greco-bundle')), $context
  132.         )->first();
  133.         if ($cfsId) {
  134.             $cfsRepo->delete([
  135.                 ['id' => $cfsId->getId()]
  136.             ], $context);
  137.             //delete custom_field entries
  138.             $cfIds $cfRepo->search((new Criteria())
  139.                 ->addFilter(
  140.                     new EqualsFilter('customFieldSetId'$cfsId->getId())
  141.                 ), $context)->getIds();
  142.             $ids = [];
  143.             foreach ($cfIds as $id) {
  144.                 $ids[] = ['id' => $id];
  145.             }
  146.             $cfRepo->delete($ids$context);
  147.         }
  148.     }
  149. }