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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ks\KsAdditions;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  9. use Shopware\Core\System\CustomField\CustomFieldTypes;
  10. /**
  11.  * Class KsAdditions
  12.  * @package Ks\KsAdditions
  13.  */
  14. class KsAdditions extends Plugin
  15. {
  16.     const PRODUCT_SET 'ks_product_set';
  17.     public const ADDVISOR_PASSUNG_MIN 'addvisor_passung_min';
  18.     public const ADDVISOR_PASSUNG_MAX 'addvisor_passung_max';
  19.     public const MIGRATION_ADDVISOR_PASSUNG 'migration_geuther_product_addvisor_passung';
  20.     const CATEGORY_SET 'ks_category_set';
  21.     public const HIDE_IN_NAVIGATION 'hide_in_navigation';
  22.     public const SWITCH_OFF_ALL_CUSTOM_FILTERS 'switch_off_all_custom_filters';
  23.     /**
  24.      * @param InstallContext $installContext
  25.      */
  26.     public function install(InstallContext $installContext): void
  27.     {
  28.         parent::install($installContext);
  29.         // $this->clean();
  30.         $this->installCustomFields($installContext->getContext());
  31.     }
  32.     /**
  33.      * @param UpdateContext $updateContext
  34.      */
  35.     public function update(UpdateContext $updateContext): void
  36.     {
  37.         parent::update($updateContext);
  38.         $currentVersion $updateContext->getCurrentPluginVersion();
  39.         if (version_compare($currentVersion'1.0.1''<')) {
  40.             $this->installProductCustomFields($updateContext->getContext());
  41.         }
  42.         if (version_compare($currentVersion'1.0.2''<')) {
  43.             $this->installCategoryCustomFields($updateContext->getContext());
  44.         }
  45.     }
  46.     protected function clean(): void
  47.     {
  48.         $connection $this->container->get(Connection::class);
  49.         $connection->executeQuery(
  50.             "DELETE FROM custom_field_set WHERE name = '" . static::PRODUCT_SET "'"
  51.         );
  52.         $connection->executeQuery(
  53.             "DELETE FROM custom_field_set WHERE name = '" . static::CATEGORY_SET "'"
  54.         );
  55.     }
  56.     /**
  57.      * @param Context $context
  58.      */
  59.     private function installCustomFields(Context $context)
  60.     {
  61.         $this->installProductCustomFields($context);
  62.         $this->installCategoryCustomFields($context);
  63.     }
  64.     /**
  65.      * @param Context $context
  66.      */
  67.     private function installProductCustomFields(Context $context)
  68.     {
  69.         $customFieldsArray[] = [
  70.             'name' => self::ADDVISOR_PASSUNG_MIN,
  71.             'type' => CustomFieldTypes::FLOAT,
  72.             'config' => [
  73.                 'customFieldPosition' => 1,
  74.                 'label' => [
  75.                     'en-GB' => 'Addvisor passung min, cm',
  76.                     'de-DE' => 'Addvisor passung min, cm'
  77.                 ],
  78.             ]
  79.         ];
  80.         $customFieldsArray[] = [
  81.             'name' => self::ADDVISOR_PASSUNG_MAX,
  82.             'type' => CustomFieldTypes::FLOAT,
  83.             'config' => [
  84.                 'customFieldPosition' => 2,
  85.                 'label' => [
  86.                     'en-GB' => 'Addvisor passung max, cm',
  87.                     'de-DE' => 'Addvisor passung max, cm'
  88.                 ],
  89.             ]
  90.         ];
  91.         $customFieldsRepository $this->container->get('custom_field_set.repository');
  92.         $customFieldsRepository->upsert([
  93.             [
  94.                 'name' => static::PRODUCT_SET,
  95.                 'global' => false,
  96.                 'config' => [
  97.                     'label' => [
  98.                         'de-DE' => 'Product custom field',
  99.                         'en-GB' => 'Product custom field'
  100.                     ],
  101.                 ],
  102.                 'relations' => [
  103.                     [
  104.                         'entityName' => 'product'
  105.                     ]
  106.                 ],
  107.                 'customFields' => $customFieldsArray
  108.             ]
  109.         ], $context);
  110.     }
  111.     /**
  112.      * @param Context $context
  113.      */
  114.     private function installCategoryCustomFields(Context $context)
  115.     {
  116.         $customFieldsRepository $this->container->get('custom_field_set.repository');
  117.         $customFieldsRepository->upsert([
  118.             [
  119.                 'name' => static::CATEGORY_SET,
  120.                 'global' => false,
  121.                 'config' => [
  122.                     'label' => [
  123.                         'de-DE' => 'Category custom fields',
  124.                         'en-GB' => 'Category custom fields'
  125.                     ],
  126.                 ],
  127.                 'relations' => [
  128.                     [
  129.                         'entityName' => 'category'
  130.                     ]
  131.                 ],
  132.                 'customFields' => [
  133.                     [
  134.                         'name' => static::CATEGORY_SET '_' self::HIDE_IN_NAVIGATION,
  135.                         'type' => CustomFieldTypes::SWITCH,
  136.                         'config' => [
  137.                             'customFieldPosition' => 1,
  138.                             'label' => [
  139.                                 'en-GB' => 'Hide in navigation (in current lang)',
  140.                                 'de-DE' => 'In der Navigation ausblenden (in der aktuellen Sprache)'
  141.                             ],
  142.                         ]
  143.                     ],
  144.                     [
  145.                         'name' => static::CATEGORY_SET '_' self::SWITCH_OFF_ALL_CUSTOM_FILTERS,
  146.                         'type' => CustomFieldTypes::SWITCH,
  147.                         'config' => [
  148.                             'customFieldPosition' => 2,
  149.                             'label' => [
  150.                                 'en-GB' => 'Switch off all custom filters',
  151.                                 'de-DE' => 'Alle benutzerdefinierten Filter ausschalten'
  152.                             ],
  153.                         ]
  154.                     ]
  155.                 ]
  156.             ]
  157.         ], $context);
  158.     }
  159. }