custom/plugins/AcrisCheckoutMethodPreselectCS/src/AcrisCheckoutMethodPreselectCS.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CheckoutMethodPreselect;
  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\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. use Shopware\Core\System\Snippet\SnippetEntity;
  15. class AcrisCheckoutMethodPreselectCS extends Plugin
  16. {
  17.     /**
  18.      * @deprecated CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT
  19.      */
  20.     const CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT 'acris_checkout_payment_shipping_preselect';
  21.     const CUSTOM_FIELD_SET_NAME_SHIPPING 'acris_checkout_shipping_preselect';
  22.     const CUSTOM_FIELD_SET_NAME_PAYMENT 'acris_checkout_payment_preselect';
  23.     const CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY 'acris_checkout_payment_preselect_priority';
  24.     const CUSTOM_FIELD_SHIPPING_PRESELECT_PRIORITY 'acris_checkout_shipping_preselect_priority';
  25.     public function uninstall(UninstallContext $context): void
  26.     {
  27.         parent::uninstall($context);
  28.         if ($context->keepUserData()) {
  29.             return;
  30.         }
  31.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENTself::CUSTOM_FIELD_SET_NAME_SHIPPINGself::CUSTOM_FIELD_SET_NAME_PAYMENT]);
  32.     }
  33.     public function install(InstallContext $context): void
  34.     {
  35.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENT]);
  36.         $this->addCustomFields($context->getContext());
  37.     }
  38.     public function update(UpdateContext $updateContext): void
  39.     {
  40.         if((version_compare($updateContext->getCurrentPluginVersion(), '3.0.0''<') && version_compare($updateContext->getUpdatePluginVersion(), '3.0.0''>='))) {
  41.             $this->removeCustomFields($updateContext->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_PAYMENTself::CUSTOM_FIELD_SET_NAME_SHIPPINGself::CUSTOM_FIELD_SET_NAME_PAYMENT]);
  42.             $this->addCustomFields($updateContext->getContext());
  43.         }
  44.     }
  45.     private function addCustomFields(Context $context): void
  46.     {
  47.         /* Check for snippets if they exist for custom fields */
  48.         $this->checkForExistingCustomFieldSnippets($context);
  49.         $customFieldSet $this->container->get('custom_field_set.repository');
  50.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_SHIPPING)), $context)->count() == 0) {
  51.             $customFieldSet->create([[
  52.                 'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING,
  53.                 'config' => [
  54.                     'label' => [
  55.                         'en-GB' => 'Shipping preselect in checkout process',
  56.                         'de-DE' => 'Vorauswahl der Versandart im Bestellprozess'
  57.                     ]
  58.                 ],
  59.                 'relations' => [
  60.                     [
  61.                         'entityName' => 'shipping_method'
  62.                     ]
  63.                 ],
  64.                 'customFields' => [
  65.                     ['name' => self::CUSTOM_FIELD_SHIPPING_PRESELECT_PRIORITY'type' => CustomFieldTypes::INT,
  66.                         'config' => [
  67.                             'componentName' => 'sw-field',
  68.                             'type' => 'number',
  69.                             'numberType' => 'int',
  70.                             'customFieldType' => 'number',
  71.                             'customFieldPosition' => 1,
  72.                             'label' => [
  73.                                 'en-GB' => 'Shpping preselection priority',
  74.                                 'de-DE' => 'Versandart Vorauswahl Priorität'
  75.                             ]
  76.                         ]]
  77.                 ],
  78.             ]], $context);
  79.         }
  80.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_PAYMENT)), $context)->count() == 0) {
  81.             $customFieldSet->create([[
  82.                 'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING,
  83.                 'config' => [
  84.                     'label' => [
  85.                         'en-GB' => 'Payment preselect in checkout process',
  86.                         'de-DE' => 'Vorauswahl der Zahlungsart im Bestellprozess'
  87.                     ]
  88.                 ],
  89.                 'relations' => [
  90.                     [
  91.                         'entityName' => 'payment_method'
  92.                     ]
  93.                 ],
  94.                 'customFields' => [
  95.                     ['name' => self::CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY'type' => CustomFieldTypes::INT,
  96.                         'config' => [
  97.                             'componentName' => 'sw-field',
  98.                             'type' => 'number',
  99.                             'numberType' => 'int',
  100.                             'customFieldType' => 'number',
  101.                             'customFieldPosition' => 1,
  102.                             'label' => [
  103.                                 'en-GB' => 'Payment preselection priority',
  104.                                 'de-DE' => 'Zahlungsart Vorauswahl Priorität'
  105.                             ]
  106.                         ]]
  107.                 ],
  108.             ]], $context);
  109.         }
  110.     }
  111.     private function removeCustomFields(Context $context, array $setNames): void
  112.     {
  113.         /* Check for snippets if they exist for custom fields */
  114.         $this->checkForExistingCustomFieldSnippets($context);
  115.         $customFieldSet $this->container->get('custom_field_set.repository');
  116.         foreach ($setNames as $setName) {
  117.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  118.             if($id$customFieldSet->delete([['id' => $id]], $context);
  119.         }
  120.     }
  121.     private function checkForExistingCustomFieldSnippets(Context $context)
  122.     {
  123.         /** @var EntityRepositoryInterface $snippetRepository */
  124.         $snippetRepository $this->container->get('snippet.repository');
  125.         $criteria = new Criteria();
  126.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  127.             new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY),
  128.             new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_SHIPPING_PRESELECT_PRIORITY)
  129.         ]));
  130.         /** @var EntitySearchResult $searchResult */
  131.         $searchResult $snippetRepository->search($criteria$context);
  132.         if ($searchResult->count() > 0) {
  133.             $snippetIds = [];
  134.             /** @var SnippetEntity $snippet */
  135.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  136.                 $snippetIds[] = [
  137.                     'id' => $snippet->getId()
  138.                 ];
  139.             }
  140.             if (!empty($snippetIds)) {
  141.                 $snippetRepository->delete($snippetIds$context);
  142.             }
  143.         }
  144.     }
  145. }