vendor/store.shopware.com/watodigitecgalaxusexport/src/WatoDigitecGalaxusExport.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wato\DigitecGalaxusExport;
  3. use Shopware\Core\Content\ProductExport\ProductExportEntity;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  14. class WatoDigitecGalaxusExport extends Plugin
  15. {
  16.     const TEMPLATE_NAMES = array(
  17.         'ProductData',
  18.         'ProductProperties',
  19.         'ProductAccessory',
  20.         'ProductStockPricing',
  21.     );
  22.     public function install(InstallContext $installContext): void {
  23.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  24.         $customFieldSetRepository->create([
  25.             [
  26.                 'name' => 'wato_digitec_galaxus_export',
  27.                 'config' => [
  28.                     'label' => [
  29.                         'de-DE' => 'WATO Digitec Galaxus Export'
  30.                     ]
  31.                 ],
  32.                 'customFields' => [
  33.                     [
  34.                         'name' => 'wato_digitec_galaxus_export_tares',
  35.                         'type' => CustomFieldTypes::TEXT,
  36.                         'config' => [
  37.                             'label' => [
  38.                                 'de-DE' => 'TARES'
  39.                             ],
  40.                             'type' => 'text',
  41.                             'customFieldType' => 'text',
  42.                             'placeholder' => [
  43.                                 'de-DE' => 'Schweizer Zolltarifnummer'
  44.                             ],
  45.                             'customFieldPosition' => 1
  46.                         ]
  47.                     ],
  48.                     [
  49.                         'name' => 'wato_digitec_galaxus_export_taric',
  50.                         'type' => CustomFieldTypes::TEXT,
  51.                         'config' => [
  52.                             'label' => [
  53.                                 'de-DE' => 'TARIC'
  54.                             ],
  55.                             'type' => 'text',
  56.                             'customFieldType' => 'text',
  57.                             'placeholder' => [
  58.                                 'de-DE' => 'Warencode für den EU-Raum'
  59.                             ],
  60.                             'customFieldPosition' => 2
  61.                         ]
  62.                     ]
  63.                 ],
  64.                 'relations' => [
  65.                     [
  66.                         'id' => '01230000000000000000000000000000',
  67.                         'entityName' => 'product'
  68.                     ]
  69.                 ]
  70.             ]
  71.         ], $installContext->getContext());
  72.         // use existing storefront configuration
  73.         $salesChannelRepository $this->container->get('sales_channel.repository');
  74.         $store $salesChannelRepository->search(
  75.             (new Criteria())->addFilter(
  76.                 new EqualsFilter('type.id'Defaults::SALES_CHANNEL_TYPE_STOREFRONT)
  77.             )->addAssociation('domains'),
  78.             $installContext->getContext()
  79.         )->first();
  80.         if (!$store) {
  81.             return;
  82.         }
  83.         $domain $store->getDomains() ? $store->getDomains()->first() : null;
  84.         if (!$domain) {
  85.             return;
  86.         }
  87.         $data = array();
  88.         foreach (static::TEMPLATE_NAMES as $template) {
  89.             $data[] = [
  90.                 'name'                          => 'Galaxus ' $template ' Export',
  91.                 'typeId'                        => Defaults::SALES_CHANNEL_TYPE_PRODUCT_COMPARISON,
  92.                 'languageId'                    => $store->getLanguageId(),
  93.                 'customerGroupId'               => $store->getCustomerGroupId(),
  94.                 'currencyId'                    => $store->getCurrencyId(),
  95.                 'paymentMethodId'               => $store->getPaymentMethodId(),
  96.                 'shippingMethodId'              => $store->getShippingMethodId(),
  97.                 'countryId'                     => $store->getCountryId(),
  98.                 'navigationCategoryId'          => $store->getNavigationCategoryId(),
  99.                 'navigationCategoryIdVersionId' => Defaults::LIVE_VERSION,
  100.                 'accessKey'                     => 'WDGE' strtoupper(str_replace(['+''/''='], ['-''_'''], base64_encode(random_bytes(16)))),
  101.                 'productExports' => [
  102.                     [
  103.                         'productStreamId'          => $this->getProductStream($template$installContext->getContext())->getId(),
  104.                         'storefrontSalesChannelId' => $store->getId(),
  105.                         'interval'                 => 60,
  106.                         'salesChannelDomainId'     => $domain->getId(),
  107.                         'currencyId'               => $store->getCurrencyId(),
  108.                         'fileName'                 => $template '.csv',
  109.                         'accessKey'                => 'WDGE' strtoupper(str_replace(['+''/''='], ['-''_'''], base64_encode(random_bytes(16)))),
  110.                         'encoding'                 => ProductExportEntity::ENCODING_UTF8,
  111.                         'fileFormat'               => ProductExportEntity::FILE_FORMAT_CSV,
  112.                         'headerTemplate'           => file_get_contents(__DIR__ '/Resources/Exports/' $template '_Header.txt'),
  113.                         'bodyTemplate'             => file_get_contents(__DIR__ '/Resources/Exports/' $template '_Template.txt'),
  114.                         'footerTemplate'           => '',
  115.                         'includeVariants'          => true,
  116.                         'generateByCronjob'        => true,
  117.                     ]
  118.                 ]
  119.             ];
  120.         }
  121.         $salesChannelRepository->create($data$installContext->getContext());
  122.         
  123.     }
  124.     public function uninstall(UninstallContext $uninstallContext): void {
  125.         $salesChannelRepository $this->container->get('sales_channel.repository');
  126.         $productStreamRepository $this->container->get('product_stream.repository');
  127.         foreach (static::TEMPLATE_NAMES as $templateName) {
  128.             $salesChannel $salesChannelRepository->search(
  129.                 (new Criteria())->addFilter(
  130.                     new EqualsFilter('name''Galaxus ' $templateName ' Export')
  131.                 )->addAssociation('domains'),
  132.                 $uninstallContext->getContext()
  133.             )->first();
  134.             if ($salesChannel) {
  135.                 $salesChannelRepository->delete(
  136.                     array(array('id' => $salesChannel->getId())),
  137.                     $uninstallContext->getContext()
  138.                 );
  139.             }
  140.             $productStream $this->getProductStream($templateName$uninstallContext->getContext(), false);
  141.             if ($productStream) {
  142.                 $productStreamRepository->delete(
  143.                     array(array('id' => $productStream->getId())),
  144.                     $uninstallContext->getContext()
  145.                 );
  146.             }
  147.         }
  148.         $this->removeCustomField($uninstallContext);
  149.     }
  150.     protected function getProductStream($templateName$context$create true)
  151.     {
  152.         $productStreamRepository $this->container->get('product_stream.repository');
  153.         $entities $productStreamRepository->search(
  154.             (new Criteria())->addFilter(new EqualsFilter('name''Galaxus ' $templateName)),
  155.             $context
  156.         );
  157.         if ($entities->first() === null && $create) {
  158.             $productStreamRepository->upsert([[
  159.                 'name' => 'Galaxus ' $templateName,
  160.                 'filters' => [
  161.                     [
  162.                         'type' => 'multi',
  163.                         'operator' => 'OR',
  164.                         'queries' => [
  165.                             [
  166.                                 'type' => 'multi',
  167.                                 'operator' => 'AND',
  168.                                 'queries' => [
  169.                                     [
  170.                                         'type' => 'equals',
  171.                                         'field' => 'active',
  172.                                         'value' => '1'
  173.                                     ],
  174.                                     [
  175.                                         'type' => 'multi',
  176.                                         'operator' => 'OR',
  177.                                         'queries' => [
  178.                                             [
  179.                                                 'type' => 'range',
  180.                                                 'field' => 'stock',
  181.                                                 'parameters' => [
  182.                                                     'gt' => 0
  183.                                                 ]
  184.                                             ],
  185.                                             [
  186.                                                 'type' => 'equals',
  187.                                                 'field' => 'isCloseout',
  188.                                                 'value' => '0'
  189.                                             ]
  190.                                         ]
  191.                                     ]
  192.                                 ]
  193.                             ]
  194.                         ]
  195.                     ]
  196.                 ]
  197.             ]], $context);
  198.             $entities $productStreamRepository->search(
  199.                 (new Criteria())->addFilter(new EqualsFilter('name''Galaxus ' $templateName)),
  200.                 $context
  201.             );
  202.         }
  203.         return $entities->first();
  204.     }
  205.     private function removeCustomField(UninstallContext $uninstallContext)
  206.     {
  207.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  208.         $fieldIds $this->customFieldsExist($uninstallContext->getContext());
  209.         if ($fieldIds) {
  210.             $customFieldSetRepository->delete(array_values($fieldIds->getData()), $uninstallContext->getContext());
  211.         }
  212.     }
  213.     private function customFieldsExist(Context $context): ?IdSearchResult
  214.     {
  215.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  216.         $criteria = new Criteria();
  217.         $criteria->addFilter(new EqualsAnyFilter('name', ['wato_digitec_galaxus_export']));
  218.         $ids $customFieldSetRepository->searchIds($criteria$context);
  219.         return $ids->getTotal() > $ids null;
  220.     }
  221. }