custom/plugins/AcrisShippingMethodTextCS/src/AcrisShippingMethodTextCS.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShippingMethodText;
  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\System\CustomField\CustomFieldTypes;
  13. use Shopware\Core\System\Snippet\SnippetEntity;
  14. class AcrisShippingMethodTextCS extends Plugin
  15. {
  16.     const CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY 'acris_shipping_costs_text_display';
  17.     public function install(InstallContext $context): void
  18.     {
  19.         $this->addCustomFields($context->getContext());
  20.     }
  21.     private function addCustomFields(Context $context): void
  22.     {
  23.         /* Check for snippets if they exist for custom fields */
  24.         $this->checkForExistingCustomFieldSnippets($context);
  25.         $customFieldSet $this->container->get('custom_field_set.repository');
  26.         if ($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY)), $context)->count() == 0) {
  27.             $customFieldSet->create([[
  28.                 'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY,
  29.                 'config' => [
  30.                     'label' => [
  31.                         'en-GB' => 'Shipping cost display settings',
  32.                         'de-DE' => 'Einstellungen für die Anzeige der Versandkosten'
  33.                     ]
  34.                 ],
  35.                 'relations' => [
  36.                     [
  37.                         'entityName' => 'shipping_method'
  38.                     ]
  39.                 ],
  40.                 'customFields' => [
  41.                     ['name' => 'acris_shipping_costs_text_type''type' => CustomFieldTypes::SELECT,
  42.                         'config' => [
  43.                             'componentName' => 'sw-single-select',
  44.                             'type' => CustomFieldTypes::SELECT,
  45.                             'customFieldType' => CustomFieldTypes::SELECT,
  46.                             'customFieldPosition' => 10,
  47.                             'label' => [
  48.                                 'en-GB' => 'Display shipping costs',
  49.                                 'de-DE' => 'Anzeige Versandkosten'
  50.                             ],
  51.                             'options' => [
  52.                                 [
  53.                                     'label' => [
  54.                                         'en-GB' => 'Show shipping costs (default)',
  55.                                         'de-DE' => 'Versandkosten anzeigen (Standard)'
  56.                                     ],
  57.                                     'value' => 'default'
  58.                                 ],
  59.                                 [
  60.                                     'label' => [
  61.                                         'en-GB' => 'Show text instead of shipping costs',
  62.                                         'de-DE' => 'Text anzeigen statt Versandkosten'
  63.                                     ],
  64.                                     'value' => 'show_text'
  65.                                 ]
  66.                             ]
  67.                         ]
  68.                     ],
  69.                     ['name' => 'acris_shipping_costs_text''type' => CustomFieldTypes::TEXT,
  70.                         'config' => [
  71.                             'componentName' => 'sw-field',
  72.                             'type' => CustomFieldTypes::TEXT,
  73.                             'customFieldType' => CustomFieldTypes::TEXT,
  74.                             'customFieldPosition' => 20,
  75.                             'label' => [
  76.                                 'en-GB' => 'Shipping costs text',
  77.                                 'de-DE' => 'Versandkosten Text'
  78.                             ],
  79.                             'placeholder' => [
  80.                                 'de-DE' => 'Lorem ipsum',
  81.                                 'en-GB' => 'Lorem ipsum'
  82.                             ],
  83.                             'helpText' => [
  84.                                 'de-DE' => 'Wird im Warenkorb / Checkout angezeigt, wenn bei der Anzeige Versandkosten Text anzeigen statt Versandkosten ausgewählt wurde.',
  85.                                 'en-GB' => 'Is displayed in the shopping cart / checkout, if Show text instead of shipping costs is selected in the Display shipping costs settings.'
  86.                             ]
  87.                         ]
  88.                     ]
  89.                 ]
  90.             ]], $context);
  91.         };
  92.     }
  93.     public function uninstall(UninstallContext $context): void
  94.     {
  95.         parent::uninstall($context);
  96.         if ($context->keepUserData()) {
  97.             return;
  98.         }
  99.         $this->removeCustomFields($context->getContext());
  100.     }
  101.     private function removeCustomFields(Context $context): void
  102.     {
  103.         /* Check for snippets if they exist for custom fields */
  104.         $this->checkForExistingCustomFieldSnippets($context);
  105.         $customFieldSet $this->container->get('custom_field_set.repository');
  106.         $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_SHIPPING_COSTS_DISPLAY)), $context)->firstId();
  107.         if($id$customFieldSet->delete([['id' => $id]], $context);
  108.     }
  109.     private function checkForExistingCustomFieldSnippets(Context $context)
  110.     {
  111.         /** @var EntityRepositoryInterface $snippetRepository */
  112.         $snippetRepository $this->container->get('snippet.repository');
  113.         $criteria = new Criteria();
  114.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  115.             new EqualsFilter('translationKey''customFields.' 'acris_shipping_costs_text_type'),
  116.             new EqualsFilter('translationKey''customFields.' 'acris_shipping_costs_text'),
  117.             new EqualsFilter('translationKey''customFields.' 'acris_shipping_costs_text_instructions')
  118.         ]));
  119.         /** @var EntitySearchResult $searchResult */
  120.         $searchResult $snippetRepository->search($criteria$context);
  121.         if ($searchResult->count() > 0) {
  122.             $snippetIds = [];
  123.             /** @var SnippetEntity $snippet */
  124.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  125.                 $snippetIds[] = [
  126.                     'id' => $snippet->getId()
  127.                 ];
  128.             }
  129.             if (!empty($snippetIds)) {
  130.                 $snippetRepository->delete($snippetIds$context);
  131.             }
  132.         }
  133.     }
  134. }