custom/plugins/AcrisShippingCalculationCS/src/AcrisShippingCalculationCS.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShippingCalculation;
  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\DataAbstractionLayer\Search\Filter\MultiFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. use Shopware\Core\System\Snippet\SnippetEntity;
  13. class AcrisShippingCalculationCS extends Plugin
  14. {
  15.     const CUSTOM_FIELD_SET_NAME_SHIPPING_CALCULATION 'acris_calculation';
  16.     public function install(Plugin\Context\InstallContext $context): void
  17.     {
  18.         $this->addCustomFields($context->getContext());
  19.     }
  20.     public function uninstall(Plugin\Context\UninstallContext $context): void
  21.     {
  22.         if ($context->keepUserData()) {
  23.             return;
  24.         }
  25.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_CALCULATION]);
  26.     }
  27.     private function removeCustomFields(Context $context, array $setNames): void
  28.     {
  29.         /* Check for snippets if they exist for custom fields */
  30.         $this->checkForExistingCustomFieldSnippets($context);
  31.         $customFieldSet $this->container->get('custom_field_set.repository');
  32.         foreach ($setNames as $setName) {
  33.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  34.             if($id$customFieldSet->delete([['id' => $id]], $context);
  35.         }
  36.     }
  37.     private function addCustomFields(Context $context): void
  38.     {
  39.         /* Check for snippets if they exist for custom fields */
  40.         $this->checkForExistingCustomFieldSnippets($context);
  41.         $customFieldSet $this->container->get('custom_field_set.repository');
  42.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_SHIPPING_CALCULATION)), $context)->count() == 0) {
  43.             $customFieldSet->create([[
  44.                 'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING_CALCULATION,
  45.                 'config' => [
  46.                     'label' => [
  47.                         'en-GB' => 'Shipping calculation',
  48.                         'de-DE' => 'Berechnung des Versands'
  49.                     ]
  50.                 ],
  51.                 'customFields' => [
  52.                     ['name' => 'acris_shipping_code''type' => CustomFieldTypes::HTML,
  53.                         'config' => [
  54.                             'componentName' => 'sw-field',
  55.                             'type' => 'text',
  56.                             'customFieldType' => 'text',
  57.                             'customFieldPosition' => 1,
  58.                             'label' => [
  59.                                 'en-GB' => 'Shipping calculation template',
  60.                                 'de-DE' => 'Vorlage für die Versandberechnung'
  61.                             ],
  62.                             'helpText' => [
  63.                                 'en-GB' => 'The template will be used for shipping calculation price.',
  64.                                 'de-DE' => 'Die Vorlage wird für den Preis der Versandberechnung verwendet.'
  65.                             ]
  66.                         ]],
  67.                     ['name' => 'acris_shipping_calculation''type' => CustomFieldTypes::BOOL,
  68.                         'config' => [
  69.                             'componentName' => 'sw-field',
  70.                             'type' => 'switch',
  71.                             'customFieldType' => 'switch',
  72.                             'customFieldPosition' => 1,
  73.                             'label' => [
  74.                                 'en-GB' => 'Shipping calculation active',
  75.                                 'de-DE' => 'Versandberechnung aktiv'
  76.                             ],
  77.                             'helpText' => [
  78.                                 'en-GB' => 'When shipping calculation is active, template will be used for shipping price calculation.',
  79.                                 'de-DE' => 'Wenn die Versandberechnung aktiv ist, wird die Vorlage für die Versandpreisberechnung verwendet.'
  80.                             ]
  81.                         ]]
  82.                 ]
  83.             ]], $context);
  84.         };
  85.     }
  86.     private function checkForExistingCustomFieldSnippets(Context $context)
  87.     {
  88.         /** @var EntityRepositoryInterface $snippetRepository */
  89.         $snippetRepository $this->container->get('snippet.repository');
  90.         $criteria = new Criteria();
  91.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  92.             new EqualsFilter('translationKey''customFields.' 'acris_shipping_code'),
  93.             new EqualsFilter('translationKey''customFields.' 'acris_shipping_calculation')
  94.         ]));
  95.         /** @var EntitySearchResult $searchResult */
  96.         $searchResult $snippetRepository->search($criteria$context);
  97.         if ($searchResult->count() > 0) {
  98.             $snippetIds = [];
  99.             /** @var SnippetEntity $snippet */
  100.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  101.                 $snippetIds[] = [
  102.                     'id' => $snippet->getId()
  103.                 ];
  104.             }
  105.             if (!empty($snippetIds)) {
  106.                 $snippetRepository->delete($snippetIds$context);
  107.             }
  108.         }
  109.     }
  110. }