custom/plugins/ProcOrderSimulate/src/ProcOrderSimulate.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Proc\ProcOrderSimulate;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. class ProcOrderSimulate extends Plugin
  12. {
  13.     /**
  14.      * @var string
  15.      */
  16.     private $customFieldSetName 'custom_shippingCosts';
  17.     private $customFieldNames = [
  18.         'custom_shippingCosts_freeShippingThreshold',
  19.         'custom_shippingCosts_pickupOnly'
  20.     ];
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $customFieldSetRepository;
  25.     private $customFieldRepository;
  26.     /**
  27.      * @param InstallContext $installContext
  28.      */
  29.     public function install(InstallContext $installContext): void
  30.     {
  31.         $this->initRepositories();
  32.         $context $installContext->getContext();
  33.         $criteria = new Criteria();
  34.         $criteria->addFilter(new EqualsFilter('name'$this->customFieldSetName));
  35.         $customFieldset $this->customFieldSetRepository->search($criteria$context);
  36.         /**
  37.          * Create custom field set with custom fields, when this custom field set doesn't exists yet.
  38.          */
  39.         if ($customFieldset->getTotal() == 0) {
  40.             $this->createCustomFieldSet($context);
  41.         }
  42.         foreach ($this->customFieldNames as $customFieldName) {
  43.             $criteria = new Criteria();
  44.             $criteria->addFilter(new EqualsFilter('name'$customFieldName));
  45.             $customField $this->customFieldRepository->search($criteria$context);
  46.             /**
  47.              * Create custom fields, when this custom field set already exists, but this custom fields don't
  48.              */
  49.             if ($customField->getTotal() == 0) {
  50.                 $this->createCustomField($context);
  51.             }
  52.         }
  53.     }
  54.     /**
  55.      * @param UninstallContext $uninstallContext
  56.      */
  57.     public function uninstall(UninstallContext $uninstallContext): void
  58.     {
  59.         if ($uninstallContext->keepUserData()) {
  60.             parent::uninstall($uninstallContext);
  61.             return;
  62.         }
  63.         $context $uninstallContext->getContext();
  64.         $this->initRepositories();
  65.         //        dd($uninstallContext);
  66.         /**
  67.          * Remove custom field set and custom fields
  68.          */
  69.         foreach ($this->customFieldNames as $customFieldName) {
  70.             $criteria = (new Criteria())->addFilter(new Equalsfilter('name'$customFieldName));
  71.             $customField $this->customFieldRepository->search($criteria$context);
  72.             if ($customField->getTotal() !== 0) {
  73.                 $this->customFieldRepository->delete([['id' => $customField->first()->getId()]], $context);
  74.             }
  75.         }
  76.         $criteria = (new Criteria())->addFilter(new Equalsfilter('name'$this->customFieldSetName));
  77.         $customFieldset $this->customFieldSetRepository->search($criteria$context);
  78.         if ($customFieldset->getTotal() !== 0) {
  79.             $this->customFieldSetRepository->delete([['id' => $customFieldset->first()->getId()]], $context);
  80.         }
  81.     }
  82.     /**
  83.      * Initialize necessary repositories
  84.      *
  85.      * @return void
  86.      */
  87.     private function initRepositories()
  88.     {
  89.         $this->customFieldSetRepository $this->container->get('custom_field_set.repository');
  90.         $this->customFieldRepository $this->container->get('custom_field.repository');
  91.     }
  92.     /**
  93.      * Create custom field set
  94.      *
  95.      * @param Context $context
  96.      * @return void
  97.      */
  98.     private function createCustomFieldSet(Context $context)
  99.     {
  100.         $customFieldSet = [
  101.             'name' => $this->customFieldSetName,
  102.             'config' => [
  103.                 'label' => [
  104.                     'de-DE' => 'Frachtkosten',
  105.                     'en-GB' => 'shipping costs'
  106.                 ],
  107.                 'translated' => true,
  108.             ],
  109.             'active' => true,
  110.             'relations' => [
  111.                 ['entityName' => 'order']
  112.             ],
  113.             'customFields' => [
  114.                 [
  115.                     'name' => "{$this->customFieldSetName}_freeShippingThreshold",
  116.                     'type' => CustomFieldTypes::INT,
  117.                     'config' => [
  118.                         'label' => [
  119.                             'de-DE' => 'Frachtfreigrenze',
  120.                             'en-GB' => 'Free shipping threshold'
  121.                         ],
  122.                         'customFieldPosition' => 1,
  123.                         'customFieldType' => 'int',
  124.                         'componentName' => 'sw-number-field'
  125.                     ]
  126.                 ],
  127.                 [
  128.                     'name' => "{$this->customFieldSetName}_pickupOnly",
  129.                     'type' => CustomFieldTypes::BOOL,
  130.                     'config' => [
  131.                         'label' => [
  132.                             'de-DE' => 'Nur Abholung',
  133.                             'en-GB' => 'Pickup only'
  134.                         ],
  135.                         'customFieldPosition' => 1,
  136.                         'customFieldType' => 'bool',
  137.                         'componentName' => 'sw-checkbox-field'
  138.                     ]
  139.                 ]
  140.             ]
  141.         ];
  142.         $this->customFieldSetRepository->upsert([$customFieldSet], $context);
  143.     }
  144.     /**
  145.      * Create custom fields
  146.      * @param Context $context
  147.      * @return void
  148.      */
  149.     private function createCustomField(Context $context)
  150.     {
  151.         $customField = [
  152.             [
  153.                 'name' => "{$this->customFieldSetName}_freeShippingThreshold",
  154.                 'type' => CustomFieldTypes::INT,
  155.                 'config' => [
  156.                     'label' => [
  157.                         'de-DE' => 'Frachtfreigrenze',
  158.                         'en-GB' => 'Free shipping threshold'
  159.                     ],
  160.                     'customFieldPosition' => 1,
  161.                     'customFieldType' => 'int',
  162.                     'componentName' => 'sw-number-field'
  163.                 ]
  164.             ],
  165.             [
  166.                 'name' => "{$this->customFieldSetName}_pickupOnly",
  167.                 'type' => CustomFieldTypes::BOOL,
  168.                 'config' => [
  169.                     'label' => [
  170.                         'de-DE' => 'Nur Abholung',
  171.                         'en-GB' => 'Pickup only'
  172.                     ],
  173.                     'customFieldPosition' => 1,
  174.                     'customFieldType' => 'bool',
  175.                     'componentName' => 'sw-checkbox-field'
  176.                 ]
  177.             ]
  178.         ];
  179.         $this->customFieldSetRepository->upsert([$customField], $context);
  180.     }
  181. }