custom/plugins/HuebertAccessoriesDirectly/src/HuebertAccessoriesDirectly.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Huebert\AccessoriesDirectly;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  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\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  15. use Shopware\Core\System\CustomField\CustomFieldTypes;
  16. use Doctrine\DBAL\Connection;
  17. class HuebertAccessoriesDirectly extends Plugin
  18. {
  19.     public function install(InstallContext $installContext): void
  20.     {
  21.         $this->createCustomFields($installContext->getContext());
  22.         $this->createCustomFieldsHeadline($installContext->getContext());
  23.         parent::install($installContext); // TODO: Change the autogenerated stub
  24.     }
  25.     public function uninstall(UninstallContext $uninstallContext): void
  26.     {
  27.         if ($uninstallContext->keepUserData()) {
  28.             parent::uninstall($uninstallContext);
  29.             return;
  30.         }
  31.         $this->removeCustomFields($uninstallContext->getContext());
  32.         $connection $this->container->get(Connection::class);
  33.         if ($connection->executeQuery("SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'product_cross_selling' AND column_name = 'display';")->fetchColumn()) {
  34.             $connection->executeUpdate('ALTER TABLE `product_cross_selling` DROP COLUMN `display`');
  35.         }
  36.         if ($connection->executeQuery("SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'product_cross_selling' AND column_name = 'selectable_amount';")->fetchColumn()) {
  37.             $connection->executeUpdate('ALTER TABLE `product_cross_selling` DROP COLUMN `selectable_amount`');
  38.         }
  39.         if ($connection->executeQuery("SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'product_cross_selling' AND column_name = 'same_quantity_as_main_product';")->fetchColumn()) {
  40.             $connection->executeUpdate('ALTER TABLE `product_cross_selling` DROP COLUMN `same_quantity_as_main_product`');
  41.         }
  42.         parent::uninstall($uninstallContext);
  43.     }
  44.     public function activate(ActivateContext $activateContext): void
  45.     {
  46.         parent::activate($activateContext); // TODO: Change the autogenerated stub
  47.     }
  48.     public function deactivate(DeactivateContext $deactivateContext): void
  49.     {
  50.         parent::deactivate($deactivateContext); // TODO: Change the autogenerated stub
  51.     }
  52.     public function update(UpdateContext $updateContext): void
  53.     {
  54.         $this->createCustomFieldsHeadline($updateContext->getContext());
  55.         parent::update($updateContext); // TODO: Change the autogenerated stub
  56.     }
  57.     private function createCustomFields($context){
  58.         $customFields = [
  59.             [
  60.                 'name' => 'huebert_accessory_directly_custom_fields',
  61.                 'active' => true,
  62.                 'config' => [
  63.                     'label' => [
  64.                         'en-GB' => 'Acessory',
  65.                         'de-DE' => 'Zubehör'
  66.                     ],
  67.                 ],
  68.                 'customFields' => [
  69.                     [
  70.                         'name' => 'huebert_accessory_directly_customtext',
  71.                         'type' => CustomFieldTypes::TEXT,
  72.                         'config' => [
  73.                             'label' => [
  74.                                 'en-GB' => 'Additional Text',
  75.                                 'de-DE' => 'Zusatztext',
  76.                             ]
  77.                         ]
  78.                     ],
  79.                 ],
  80.                 'relations' => [
  81.                     ['entityName' => 'product']
  82.                 ]
  83.             ],
  84.         ];
  85.         $repo $this->container->get('custom_field_set.repository');
  86.         //check if entries exist to prevent re-installation error
  87.         $customFieldSetEntry false;
  88.         $customFieldSetEntry $repo->search((new Criteria())
  89.             ->addFilter(
  90.                 new EqualsFilter('name''huebert_accessory_directly_custom_fields')
  91.             ), $context)->first();
  92.         if(!$customFieldSetEntry) {
  93.             foreach ($customFields as $customFieldSet) {
  94.                 $repo->upsert([$customFieldSet], $context);
  95.             }
  96.         }
  97.     }
  98.     private function removeCustomFields($context){
  99.         $cfsRepo $this->container->get('custom_field_set.repository');
  100.         $cfRepo $this->container->get('custom_field.repository');
  101.         $sRepo $this->container->get('snippet.repository');
  102.         $sysRepo $this->container->get('system_config.repository');
  103.         //delete custom_field_set entry
  104.         $cfsId $cfsRepo->search((new Criteria())
  105.                ->addFilter(new EqualsFilter('name''huebert_accessory_directly_custom_fields')), $context
  106.         )->first();
  107.         $cfsRepo->delete([
  108.             ['id' => $cfsId->getId()]
  109.         ], $context);
  110.         //delete custom_field entries
  111.         $cfIds $cfRepo->search((new Criteria())
  112.              ->addFilter(
  113.                  new EqualsFilter('customFieldSetId'$cfsId->getId())
  114.              ), $context)->getIds();
  115.         $ids = [];
  116.         foreach($cfIds as $id){
  117.             $ids[] = ['id' => $id];
  118.         }
  119.         $cfRepo->delete($ids$context);
  120.         //delete system_config entries
  121.         $sysIds $sysRepo->search((new Criteria())
  122.             ->addFilter(
  123.                 new ContainsFilter('configurationKey''HuebertAccessoriesDirectly.config')
  124.             ), $context)->getIds();
  125.         $configIds = [];
  126.         foreach($sysIds as $id){
  127.             $configIds[] = ['id' => $id];
  128.         }
  129.         $sysRepo->delete($configIds$context);
  130.         // delete snippet entries
  131.         $sIds $sRepo->search((new Criteria())
  132.             ->addFilter(
  133.                 new MultiFilter(
  134.                     MultiFilter::CONNECTION_OR,
  135.                     [
  136.                         new ContainsFilter('translationKey''huebert_accessory_directly_customtext'),
  137.                         new ContainsFilter('translationKey''huebert-acessories-directly')
  138.                     ]
  139.                 )
  140.             ), $context)->getIds();
  141.         $snippetIds = [];
  142.         foreach($sIds as $id){
  143.             $snippetIds[] = ['id' => $id];
  144.         }
  145.         $sRepo->delete($snippetIds$context);
  146.     }
  147.     private function createCustomFieldsHeadline($context) {
  148.         $repo $this->container->get('custom_field_set.repository');
  149.         $repoField $this->container->get('custom_field.repository');
  150.         //get current setId to add new custom fields to existing field Set
  151.         $customFieldSetEntry $repo->search((new Criteria())
  152.             ->addFilter(
  153.                 new EqualsFilter('name''huebert_accessory_directly_custom_fields')
  154.             ), $context)->first();
  155.         //get current custom field ids to avoid upsert error for existing custom fields
  156.         $customFieldTitle $repoField->search((new Criteria())
  157.             ->addFilter(
  158.                 new EqualsFilter('name''huebert_accessory_directly_title')
  159.             ), $context)->first();
  160.         $titleId null;
  161.         if($customFieldTitle) {
  162.             $titleId $customFieldTitle->get('id');
  163.         }
  164.         $customFieldSubtitle $repoField->search((new Criteria())
  165.             ->addFilter(
  166.                 new EqualsFilter('name''huebert_accessory_directly_subtitle')
  167.             ), $context)->first();
  168.         $subtitleId null;
  169.         if($customFieldSubtitle) {
  170.             $subtitleId $customFieldSubtitle->get('id');
  171.         }
  172.         $customFieldCustom $repoField->search((new Criteria())
  173.             ->addFilter(
  174.                 new EqualsFilter('name''huebert_accessory_directly_customtext')
  175.             ), $context)->first();
  176.         $customId null;
  177.         if($customFieldCustom) {
  178.             $customId $customFieldCustom->get('id');
  179.         }
  180.         $customFields = [
  181.             [
  182.                 'id' => $customFieldSetEntry->get('id'),
  183.                 'config' => [
  184.                     'label' => [
  185.                         'en-GB' => 'Accessory',
  186.                         'de-DE' => 'Zubehör'
  187.                     ],
  188.                 ],
  189.                 'customFields' => [
  190.                     [
  191.                         'id' => $titleId,
  192.                         'name' => 'huebert_accessory_directly_title',
  193.                         'type' => CustomFieldTypes::TEXT,
  194.                         'config' => [
  195.                             'label' => [
  196.                                 'en-GB' => 'Title Accessories',
  197.                                 'de-DE' => 'Überschrift Zubehör (Gilt für den Artikel, für welchen das Zubehör zugeordnet wird)',
  198.                             ],
  199.                             'customFieldPosition' => 1,
  200.                             'helpText' => [
  201.                                 'en-GB' => 'Overrides the default title above the accessory selection',
  202.                                 'de-DE' => 'Überschreibt den Standart Titel der oberhalb der Zubehör-Auswahl angezeigt wird',
  203.                             ]
  204.                         ]
  205.                     ],
  206.                     [
  207.                         'id' => $subtitleId,
  208.                         'name' => 'huebert_accessory_directly_subtitle',
  209.                         'type' => CustomFieldTypes::TEXT,
  210.                         'config' => [
  211.                             'label' => [
  212.                                 'en-GB' => 'Description Accessories',
  213.                                 'de-DE' => 'Beschreibung Zubehör (Gilt für den Artikel, für welchen das Zubehör zugeordnet wird)',
  214.                             ],
  215.                             'customFieldPosition' => 2,
  216.                             'helpText' => [
  217.                                 'en-GB' => 'Overrides the default description above the accessory selection',
  218.                                 'de-DE' => 'Überschreibt die Standart Beschreibung die oberhalb der Zubehör-Auswahl angezeigt wird',
  219.                             ]
  220.                         ]
  221.                     ],
  222.                     [
  223.                         'id' => $customId,
  224.                         'config' => [
  225.                             'label' => [
  226.                                 'en-GB' => 'Display Name Accessories',
  227.                                 'de-DE' => 'Anzeigename Zubehör (Gilt für den eigentlichen Zubehörartikel)',
  228.                             ],
  229.                             'customFieldPosition' => 3,
  230.                             'helpText' => [
  231.                                 'en-GB' => 'Overrides the actual product title',
  232.                                 'de-DE' => 'Überschreibt den eigentlichen Produkttitel mit dem hier hinterlegten',
  233.                             ]
  234.                         ]
  235.                     ],
  236.                 ],
  237.             ],
  238.         ];
  239.         $repo->upsert($customFields,$context);
  240.     }
  241. }