custom/static-plugins/ChannelpilotTrackingSW6/src/Channelpilot.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Chann\Channelpilot;
  4. use Chann\Channelpilot\Service\ChannelpilotPaymentHandler;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  15. class Channelpilot extends Plugin
  16. {
  17.     public function install(InstallContext $context): void
  18.     {
  19.         // Ignore InstallContext and Choose defaultContext.
  20.         // installContext in backend-based install has no system privileges,
  21.         // so addPaymentMethod() fails.
  22.         $systemContext Context::createDefaultContext();
  23.         $this->addPaymentMethod(true$systemContext);
  24.         $this->addCPPaymentToSalesChannels($systemContext);
  25.         $this->addOrderCustomFields($context->getContext());
  26.     }
  27.     public function update(UpdateContext $updateContext): void
  28.     {
  29.         $this->addOrderCustomFields($updateContext->getContext());
  30.         $this->addPaymentMethod(true$updateContext->getContext());
  31.     }
  32.     protected function addPaymentMethod(bool $activeContext $context): void
  33.     {
  34.         /** @var EntityRepositoryInterface $paymentRepository */
  35.         $paymentRepository $this->container->get('payment_method.repository');
  36.         $paymentRepository->upsert($this->getPaymentDefinitions($context), $context);
  37.     }
  38.     protected function getPaymentDefinitions($context): array
  39.     {
  40.         /** @var PluginIdProvider $pluginIdProvider */
  41.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  42.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(get_class($this), $context);
  43.         return [
  44.             [
  45.                 'id' => md5('ChannelPilot'),
  46.                 'handlerIdentifier' => ChannelpilotPaymentHandler::class,
  47.                 'pluginId' => $pluginId,
  48.                 'afterOrderEnabled' => false,
  49.                 'active' => false,
  50.                 'translations' => [
  51.                     'de-DE' => [
  52.                         'name' => 'Channel Pilot Pro',
  53.                     ],
  54.                     'en-GB' => [
  55.                         'name' => 'Channel Pilot Pro',
  56.                     ],
  57.                 ],
  58.             ]
  59.         ];
  60.     }
  61.     /**
  62.      * Create custom fields.
  63.      * All IDs fixed for idempotency on reinstallations.
  64.      */
  65.     protected function addOrderCustomFields(Context $context): void
  66.     {
  67.         $custom_field_set_id md5('Channelpilot custom_field_set');
  68.         $this->container->get('custom_field_set.repository')->upsert(
  69.             [
  70.                 [
  71.                     'id' => $custom_field_set_id,
  72.                     'name' => 'custom_channelpilot',
  73.                     'config' => [
  74.                         "label" => ["de-DE" => "Channel Pilot Pro""en-GB" => "Channel Pilot Pro"],
  75.                         "translated" => false
  76.                     ],
  77.                     'active' => true,
  78.                     'global' => false,
  79.                 ],
  80.             ],
  81.             $context
  82.         );
  83.         $this->container->get('custom_field_set_relation.repository')->upsert(
  84.             [
  85.                 [
  86.                     'id' => md5('ChannelPilot custom_field_set_relation'),
  87.                     'customFieldSetId' => $custom_field_set_id,
  88.                     'entityName' => 'order',
  89.                 ],
  90.             ],
  91.             $context
  92.         );
  93.         $this->container->get('custom_field.repository')->upsert(
  94.             [
  95.                 [
  96.                     'id' => md5('custom_channelpilot_token'),
  97.                     'customFieldSetId' => $custom_field_set_id,
  98.                     'name' => 'custom_channelpilot_token',
  99.                     'type' => 'text',
  100.                     'config' => [
  101.                         'type' => 'text',
  102.                         "label" => ["de-DE" => "Channel Pilot Pro Token für Storno und Versand""en-GB" => "Channel Pilot Pro Token for cancellation and shipping"],
  103.                         "translated" => false,
  104.                         "componentName" => "sw-field",
  105.                         "customFieldType" => "text",
  106.                         "customFieldPosition" => 1
  107.                     ],
  108.                     'active' => true,
  109.                 ],
  110.                 [
  111.                     'id' => md5('custom_channelpilot_orderjson'),
  112.                     'customFieldSetId' => $custom_field_set_id,
  113.                     'name' => 'custom_channelpilot_orderjson',
  114.                     'type' => 'text',
  115.                     'config' => [
  116.                         'type' => 'text',
  117.                         "label" => ["de-DE" => "Channel Pilot Pro serialisierte Bestellung""en-GB" => "Channel Pilot Pro serialized order"],
  118.                         "translated" => false,
  119.                         "componentName" => "sw-field",
  120.                         "customFieldType" => "text",
  121.                         "customFieldPosition" => 1
  122.                     ],
  123.                     'active' => true,
  124.                 ],
  125.                 [
  126.                     'id' => md5('custom_channelpilot_external_order_id'),
  127.                     'customFieldSetId' => $custom_field_set_id,
  128.                     'name' => 'custom_channelpilot_external_order_id',
  129.                     'type' => 'text',
  130.                     'config' => [
  131.                         'type' => 'text',
  132.                         "label" => ["de-DE" => "Channel Pilot Pro Marktplatz Order-ID""en-GB" => "Channel Pilot Pro Marketplace Order-ID"],
  133.                         "translated" => false,
  134.                         "componentName" => "sw-field",
  135.                         "customFieldType" => "text",
  136.                         "customFieldPosition" => 1
  137.                     ],
  138.                     'active' => true,
  139.                 ],
  140.                 [
  141.                     'id' => md5('custom_channelpilot_source'),
  142.                     'customFieldSetId' => $custom_field_set_id,
  143.                     'name' => 'custom_channelpilot_source',
  144.                     'type' => 'text',
  145.                     'config' => [
  146.                         'type' => 'text',
  147.                         "label" => ["de-DE" => "Channel Pilot Pro Quelle""en-GB" => "Channel Pilot Pro Source"],
  148.                         "translated" => false,
  149.                         "componentName" => "sw-field",
  150.                         "customFieldType" => "text",
  151.                         "customFieldPosition" => 1
  152.                     ],
  153.                     'active' => true,
  154.                 ],
  155.                 [
  156.                     'id' => md5('custom_channelpilot_delivery2send'),
  157.                     'customFieldSetId' => $custom_field_set_id,
  158.                     'name' => 'custom_channelpilot_delivery2send',
  159.                     'type' => 'text',
  160.                     'config' => [
  161.                         'type' => 'text',
  162.                         "label" => ["de-DE" => "Channel Pilot Pro Offene Versandmitteilung""en-GB" => "Channel Pilot Pro pending delivery message"],
  163.                         "translated" => false,
  164.                         "componentName" => "sw-field",
  165.                         "customFieldType" => "text",
  166.                         "customFieldPosition" => 1
  167.                     ],
  168.                     'active' => true,
  169.                 ],
  170.                 [
  171.                     'id' => md5('custom_channelpilot_cancel2send'),
  172.                     'customFieldSetId' => $custom_field_set_id,
  173.                     'name' => 'custom_channelpilot_cancel2send',
  174.                     'type' => 'text',
  175.                     'config' => [
  176.                         'type' => 'text',
  177.                         "label" => ["de-DE" => "Channel Pilot Pro Offene Stornomitteilung""en-GB" => "Channel Pilot Pro pending cancellation message"],
  178.                         "translated" => false,
  179.                         "componentName" => "sw-field",
  180.                         "customFieldType" => "text",
  181.                         "customFieldPosition" => 1
  182.                     ],
  183.                     'active' => true,
  184.                 ],
  185.                 [
  186.                     'id' => md5('custom_channelpilot_refund2send'),
  187.                     'customFieldSetId' => $custom_field_set_id,
  188.                     'name' => 'custom_channelpilot_refund2send',
  189.                     'type' => 'text',
  190.                     'config' => [
  191.                         'type' => 'text',
  192.                         "label" => ["de-DE" => "Channel Pilot Pro Offene Erstattung""en-GB" => "Channel Pilot Pro pending refund message"],
  193.                         "translated" => false,
  194.                         "componentName" => "sw-field",
  195.                         "customFieldType" => "text",
  196.                         "customFieldPosition" => 1
  197.                     ],
  198.                     'active' => true,
  199.                 ],
  200.             ],
  201.             $context
  202.         );
  203.     }
  204.     protected function addCPPaymentToSalesChannels(Context $context): void
  205.     {
  206.         foreach ($this->getPaymentDefinitions($context) as $paymentDefinition) {
  207.             $paymentMethodId $paymentDefinition['id'];
  208.             $salesChannelsToChange $this->getSalesChannelsToChange($context);
  209.             $updateData = [];
  210.             foreach ($salesChannelsToChange as $salesChannel) {
  211.                 $salesChannelUpdateData = [
  212.                     'id' => $salesChannel->getId(),
  213.                 ];
  214.                 $paymentMethodCollection $salesChannel->getPaymentMethods();
  215.                 if ($paymentMethodCollection === null || $paymentMethodCollection->get($paymentMethodId) === null) {
  216.                     $salesChannelUpdateData['paymentMethods'][] = [
  217.                         'id' => $paymentMethodId,
  218.                     ];
  219.                 }
  220.                 $updateData[] = $salesChannelUpdateData;
  221.             }
  222.             $this->container->get('sales_channel.repository')->update($updateData$context);
  223.         }
  224.     }
  225.     protected function getSalesChannelsToChange(Context $context): EntityCollection
  226.     {
  227.         $criteria = new Criteria();
  228.         $criteria->addFilter(
  229.             new EqualsAnyFilter('typeId', [
  230.                 Defaults::SALES_CHANNEL_TYPE_STOREFRONT,
  231.                 Defaults::SALES_CHANNEL_TYPE_API,
  232.             ])
  233.         );
  234.         $criteria->addAssociation('paymentMethods');
  235.         return $this->container->get('sales_channel.repository')->search($criteria$context)->getEntities();
  236.     }
  237. }