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.                             'helpText' => [
  77.                                 'de-DE' => 'Je höher die Priorität, desto weiter Vorne wird die Versandart bei der automatischen Auswahl gereiht.',
  78.                                 'en-GB' => 'The higher the priority, the further forward the shipping method is ranked in the automatic selection.'
  79.                             ]
  80.                         ]]
  81.                 ],
  82.             ]], $context);
  83.         }
  84.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_PAYMENT)), $context)->count() == 0) {
  85.             $customFieldSet->create([[
  86.                 'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING,
  87.                 'config' => [
  88.                     'label' => [
  89.                         'en-GB' => 'Payment preselect in checkout process',
  90.                         'de-DE' => 'Vorauswahl der Zahlungsart im Bestellprozess'
  91.                     ]
  92.                 ],
  93.                 'relations' => [
  94.                     [
  95.                         'entityName' => 'payment_method'
  96.                     ]
  97.                 ],
  98.                 'customFields' => [
  99.                     ['name' => self::CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY'type' => CustomFieldTypes::INT,
  100.                         'config' => [
  101.                             'componentName' => 'sw-field',
  102.                             'type' => 'number',
  103.                             'numberType' => 'int',
  104.                             'customFieldType' => 'number',
  105.                             'customFieldPosition' => 1,
  106.                             'label' => [
  107.                                 'en-GB' => 'Payment preselection priority',
  108.                                 'de-DE' => 'Zahlungsart Vorauswahl Priorität'
  109.                             ],
  110.                             'helpText' => [
  111.                                 'de-DE' => 'Je höher die Priorität, desto weiter Vorne wird die Zahlungsart bei der automatischen Auswahl gereiht.',
  112.                                 'en-GB' => 'The higher the priority, the further forward the payment method is ranked in the automatic selection.'
  113.                             ]
  114.                         ]]
  115.                 ],
  116.             ]], $context);
  117.         }
  118.     }
  119.     private function removeCustomFields(Context $context, array $setNames): void
  120.     {
  121.         /* Check for snippets if they exist for custom fields */
  122.         $this->checkForExistingCustomFieldSnippets($context);
  123.         $customFieldSet $this->container->get('custom_field_set.repository');
  124.         foreach ($setNames as $setName) {
  125.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  126.             if($id$customFieldSet->delete([['id' => $id]], $context);
  127.         }
  128.     }
  129.     private function checkForExistingCustomFieldSnippets(Context $context)
  130.     {
  131.         /** @var EntityRepositoryInterface $snippetRepository */
  132.         $snippetRepository $this->container->get('snippet.repository');
  133.         $criteria = new Criteria();
  134.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  135.             new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_PAYMENT_PRESELECT_PRIORITY),
  136.             new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_SHIPPING_PRESELECT_PRIORITY)
  137.         ]));
  138.         /** @var EntitySearchResult $searchResult */
  139.         $searchResult $snippetRepository->search($criteria$context);
  140.         if ($searchResult->count() > 0) {
  141.             $snippetIds = [];
  142.             /** @var SnippetEntity $snippet */
  143.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  144.                 $snippetIds[] = [
  145.                     'id' => $snippet->getId()
  146.                 ];
  147.             }
  148.             if (!empty($snippetIds)) {
  149.                 $snippetRepository->delete($snippetIds$context);
  150.             }
  151.         }
  152.     }
  153. }