custom/plugins/FourtwosixThemeCustomization/src/FourtwosixThemeCustomization.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace FourtwosixThemeCustomization;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. class FourtwosixThemeCustomization extends Plugin
  15. {
  16.     public const ATTRIBUTE_SET_NAME "custom_navigation_identity";
  17.     public const ATTRIBUTE_SET_LABEL_EN "Category Identification (426)";
  18.     public const ATTRIBUTE_SET_LABEL_DE "Kategorien Identifikation (426)";
  19.     public const CATEGORY_TYPE_ATTRIBUTE_NAME "custom_navigation_identity_category_type";
  20.     public const CATEGORY_TYPE_ATTRIBUTE_LABEL_EN "Category types assignment";
  21.     public const CATEGORY_TYPE_ATTRIBUTE_LABEL_DE "Kategorietypenzuordung";
  22.     public const TWO_COLUMNS_ATTRIBUTE_NAME "custom_navigation_identity_columns2";
  23.     public const TWO_COLUMNS_ATTRIBUTE_LABEL_EN "Category with 2 columns";
  24.     public const TWO_COLUMNS_ATTRIBUTE_LABEL_DE "Kategorie mit 2 Spalten";
  25.     public const ONLY_MOBILE_ATTRIBUTE_NAME "custom_navigation_identity_onlyMobile";
  26.     public const ONLY_MOBILE_ATTRIBUTE_LABEL_EN "show on Mobile";
  27.     public const ONLY_MOBILE_ATTRIBUTE_LABEL_DE "Angezeigt nur Mobile";
  28.     /* @fourtwosix-edit: AC https://app.asana.com/0/1202091300644180/1204101678103036/f */
  29.     public const AFFECTED_CATEGORY_ID 'FourtwosixThemeCustomization.config.affectedCategoryId';
  30.     public const HIDE_SUB_CATEGORIES 'FourtwosixThemeCustomization.config.hideSubCategories';
  31.     public const HIDDEN_CATEGORY_ID 'FourtwosixThemeCustomization.config.hiddenCategoryId';
  32.     /* @fourtwosix-edit: AC https://app.asana.com/0/1202091300644180/1204080601957727/f */
  33.     const GUEST_RULE_ID '9126c4c1239d4058afe53a8e345175d3'// Shopkunden RULE ID
  34.     const CUSTOMER_RULE_ID '6d01f06cf4c147b690f7658336ef9563'// Shopkunden 2017 RULE ID
  35.     /* @fourtwosix-edit: AC https://app.asana.com/0/1202091300644180/1203817432613932/f */
  36.     public const CUSTOMER_GROUP_TRADER_ID '698a7ea2cc7e4d05a961816644a0e27b';
  37.     public const CUSTOMER_GROUP_FOREIGN_TRADER_ID '94023f1ea4524e43b37aaef6c74c6853';
  38.     public const MANUFACTURER_PLATINLUX_ID 'c7d3d3af44fa47b6974f61879953392a';
  39.     public const MANUFACTURER_LUCIDE_ID 'f858c0cc167f422daab4dfdf77ab92d3';
  40.     public const REGISTERED_CUSTOMER_GROUP_ID '46840a25121f47c49af3e8ea0086cc3e';
  41.     public function install(InstallContext $installContext): void
  42.     {
  43.         $this->createCustomFields($installContext->getContext());
  44.     }
  45.     public function uninstall(UninstallContext $uninstallContext): void
  46.     {
  47.         if ($uninstallContext->keepUserData()) {
  48.             return;
  49.         }
  50.         $this->deleteCustomFields($uninstallContext->getContext());
  51.     }
  52.     private function createCustomFields(Context $context): void
  53.     {
  54.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  55.         $criteria = new Criteria();
  56.         $criteria->addFilter(new EqualsFilter('name'self::ATTRIBUTE_SET_NAME));
  57.         if ($customFieldSetRepository->search($criteria$context)->first()) {
  58.             return;
  59.         }
  60.         $customFieldData $this->getCustomFieldData();
  61.         $customFieldSetRepository->upsert([$customFieldData], $context);
  62.     }
  63.     private function deleteCustomFields(Context $context): void
  64.     {
  65.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  66.         if (!$customFields $this->getCustomFields($customFieldSetRepository$context)) {
  67.             return;
  68.         }
  69.         $customFieldSetRepository->delete(array_values($customFields->getData()), $context);
  70.     }
  71.     private function getCustomFields(EntityRepository &$customFieldSetRepositoryContext &$context): ?IdSearchResult
  72.     {
  73.         $criteria = new Criteria();
  74.         $criteria->addFilter(new EqualsAnyFilter('name', [self::ATTRIBUTE_SET_NAME]));
  75.         $customFields $customFieldSetRepository->searchIds($criteria$context);
  76.         var_dump($customFields);
  77.         return $customFields->getTotal() > $customFields null;
  78.     }
  79.     private function getCustomFieldData(): array
  80.     {
  81.         return [
  82.             'name' => self::ATTRIBUTE_SET_NAME,
  83.             'config' => [
  84.                 'label' => [
  85.                     'en-GB' => self::ATTRIBUTE_SET_LABEL_EN,
  86.                     'de-DE' => self::ATTRIBUTE_SET_LABEL_DE,
  87.                 ]
  88.             ],
  89.             'customFields' => [
  90.                 $this->getCustomFieldSelect(
  91.                     self::CATEGORY_TYPE_ATTRIBUTE_NAME,
  92.                     CustomFieldTypes::SELECT,
  93.                     self::CATEGORY_TYPE_ATTRIBUTE_LABEL_EN,
  94.                     self::CATEGORY_TYPE_ATTRIBUTE_LABEL_DE,
  95.                     1
  96.                 ),
  97.                 $this->getCustomField(
  98.                     self::TWO_COLUMNS_ATTRIBUTE_NAME,
  99.                     CustomFieldTypes::SWITCH,
  100.                     self::TWO_COLUMNS_ATTRIBUTE_LABEL_EN,
  101.                     self::TWO_COLUMNS_ATTRIBUTE_LABEL_DE,
  102.                     2
  103.                 ),
  104.                 $this->getCustomField(
  105.                     self::ONLY_MOBILE_ATTRIBUTE_NAME,
  106.                     CustomFieldTypes::SWITCH,
  107.                     self::ONLY_MOBILE_ATTRIBUTE_LABEL_EN,
  108.                     self::ONLY_MOBILE_ATTRIBUTE_LABEL_DE,
  109.                     3
  110.                 ),
  111.             ],
  112.             'relations' => [
  113.                 [
  114.                     'entityName' => 'category',
  115.                 ]
  116.             ],
  117.         ];
  118.     }
  119.     private function getCustomField(string $namestring $typestring $labelENstring $labelDEint $position): array
  120.     {
  121.         return [
  122.             'name' => $name,
  123.             'type' => $type,
  124.             'config' => [
  125.                 'label' => [
  126.                     'en-GB' => $labelEN,
  127.                     'de-DE' => $labelDE
  128.                 ],
  129.                 "componentName" => "sw-field",
  130.                 "customFieldType" => "switch",
  131.                 'customFieldPosition' => $position,
  132.             ],
  133.         ];
  134.     }
  135.     private function getCustomFieldSelect(string $namestring $typestring $labelENstring $labelDEint $position): array
  136.     {
  137.         return [
  138.             'name' => $name,
  139.             'type' => $type,
  140.             'config' => [
  141.                 'label' => [
  142.                     'en-GB' => $labelEN,
  143.                     'de-DE' => $labelDE
  144.                 ],
  145.                 'options' => [
  146.                     [
  147.                         'label' => [
  148.                             'en-GB' => 'First category',
  149.                             'de-DE' => 'Erste Kategorie'
  150.                         ],
  151.                         'value' => 'first_category',
  152.                     ],
  153.                     [
  154.                         'label' => [
  155.                             'en-GB' => 'Rooms category',
  156.                             'de-DE' => 'Raeume Kategorie'
  157.                         ],
  158.                         'value' => 'rooms_category',
  159.                     ],
  160.                     [
  161.                         'label' => [
  162.                             'en-GB' => 'styles category',
  163.                             'de-DE' => 'Stile Kategorie'
  164.                         ],
  165.                         'value' => 'styles_category',
  166.                     ],
  167.                     [
  168.                         'label' => [
  169.                             'en-GB' => 'Manufacturer category',
  170.                             'de-DE' => 'Hersteller Kategorie'
  171.                         ],
  172.                         'value' => 'manufacturer_category'
  173.                     ],
  174.                 ],
  175.                 "componentName" => "sw-single-select",
  176.                 "customFieldType" => "select",
  177.                 'customFieldPosition' => $position,
  178.             ],
  179.         ];
  180.     }
  181.     
  182. }