custom/plugins/ProcUploadDocument/src/ProcUploadDocument.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ProcUploadDocument;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\System\CustomField\CustomFieldTypes;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  13. class ProcUploadDocument extends Plugin {
  14.     const DOWNLOAD_URL 'custom-uploaded-document-url';
  15.     const MEDIA_ID 'custom-uploaded-document-mediaId';
  16.     const FILE_NAME 'custom-uploaded-document-fileName';
  17.     const SALES_ID 'sales-representative-identity';
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         $this->insertCustomFields($installContext->getContext());
  21.     }
  22.     public function uninstall(UninstallContext $uninstallContext): void
  23.     {
  24.         if ($uninstallContext->keepUserData()) {
  25.             return;
  26.         }
  27.         $this->deleteCustomFields($uninstallContext->getContext());
  28.     }
  29.     private function insertCustomFields(Context $context): void
  30.     {
  31.         $repo $this->container->get('custom_field_set.repository');
  32.         $criteria = new Criteria();
  33.         $criteria->addFilter(new EqualsFilter('name''order_additional_fields'));
  34.         if (!$repo->search($criteria$context)->first()) {
  35.             $attributeProduct $this->getProductCustomFields();
  36.             $result $repo->upsert([$attributeProduct], $context);
  37.         }
  38.     }
  39.     
  40.     private function deleteCustomFields(Context $context): void
  41.     {
  42.         $repo $this->container->get('custom_field_set.repository');
  43.         $fields = ['order_additional_fields'];
  44.         foreach ($fields as $field) {
  45.             $criteria = new Criteria();
  46.             $criteria->addFilter(new EqualsFilter('name'$field));
  47.             $result $repo->search($criteria$context);
  48.             if ($result != null) {
  49.                 $attributeSet $result->first();
  50.                 if ($attributeSet != null) {
  51.                     $id $attributeSet->getId();
  52.                     if ($id != null) {
  53.                         $repo->delete([['id' => $id]], $context);
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     private function getProductCustomFields(): array
  60.     {
  61.         return [
  62.             'name' => 'order_additional_fields',
  63.             'active' => true,
  64.             'config' => [
  65.                 'label' => [
  66.                     'en-GB' => 'Order additional fields',
  67.                     'de-DE' => 'Zusätzliche Bestellfelder'
  68.                 ]
  69.             ],
  70.             'customFields' => [
  71.                 [
  72.                     'name' => self::DOWNLOAD_URL,
  73.                     'type' => 'text',
  74.                     'active' => true,
  75.                     'config' => [
  76.                         'label' => [
  77.                             'en-GB' => 'Download-URL',
  78.                             'de-DE' => 'Download-URL'
  79.                         ],
  80.                         'componentName' => 'sw-field',
  81.                         'customFieldType' => 'text',
  82.                         'customFieldPosition' => 1,
  83.                         'helpText' => [
  84.                             'en-GB' => 'Url from which the file uploaded during checkout is available for viewing or downloading',
  85.                             'de-DE' => 'Url, unter der die beim Checkout hochgeladene Datei zur Ansicht oder zum Herunterladen verfügbar ist'
  86.                         ]
  87.                     ]
  88.                 ],
  89.                 [
  90.                     'name' => self::SALES_ID,
  91.                     'type' => 'text',
  92.                     'active' => true,
  93.                     'config' => [
  94.                         'label' => [
  95.                             'en-GB' => 'Agent name',
  96.                             'de-DE' => 'Agentenname'
  97.                         ],
  98.                         'componentName' => 'sw-field',
  99.                         'customFieldType' => 'text',
  100.                         'customFieldPosition' => 2,
  101.                         'helpText' => [
  102.                             'en-GB' => 'Agent name of Sales representative-identity',
  103.                             'de-DE' => 'Agentenname der Handelsvertreter-Identität'
  104.                         ]
  105.                     ]
  106.                 ],
  107.                
  108.             ],
  109.             'relations' => [
  110.                 [
  111.                     'entityName' => 'order'
  112.                 ]
  113.             ]
  114.         ];
  115.     }
  116. }