custom/plugins/fourtwosixPreOrderProduct/src/fourtwosixPreOrderProduct.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace fourtwosix\PreOrder;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. class fourtwosixPreOrderProduct extends Plugin
  11. {
  12.     public function install(InstallContext $context): void {
  13.         //create custom field for pre order flag
  14.         $repo $this->container->get('custom_field_set.repository');
  15.         $criteria = new Criteria();
  16.         $criteria->addFilter(new EqualsFilter('name''preorder'));
  17.         if(!$repo->search($criteria$context->getContext())->first()){
  18.             $id Uuid::randomHex();
  19.             //create set and custom field
  20.             $attributeSet = [
  21.                 'id' => $id,
  22.                 'name' => 'preorder',
  23.                 'active' => true,
  24.                 'config' => [
  25.                     'translated' => true,
  26.                     'label' => [
  27.                         'en-GB' => "Pre Order Configuration",
  28.                         'de-DE' => "Konfiguration vorbestellen",
  29.                         'it-IT' => "Configurazione Pre Ordine"
  30.                     ]
  31.                 ],
  32.                 'customFields' => [
  33.                     [
  34.                         'id' => Uuid::randomHex(),
  35.                         'name' => 'preorder_enabled',
  36.                         'type' => 'bool',
  37.                         'active' => true,
  38.                         'config' => [
  39.                             'customFieldType' => 'switch',
  40.                             'customFieldPosition' => 1,
  41.                             'label' => [
  42.                                 'en-GB' => "Pre order product enabled",
  43.                                 'de-DE' => "Produkt vorbestellen",
  44.                                 'it-IT' => "Pre ordine prodotto attivo"
  45.                             ],
  46.                             'helpText' => [
  47.                                 'en-GB' => "Enable product pre order based on release date",
  48.                                 'de-DE' => "Produktvorbestellung basierend auf dem Veröffentlichungsdatum aktivieren",
  49.                                 'it-IT' => "Attiva la possibilità di pre ordinare un prodotto basato sulla data di rilascio"
  50.                             ]
  51.                         ]
  52.                     ]
  53.                 ],
  54.                 'relations' => [
  55.                     [
  56.                         'entityName' => 'product',
  57.                     ]
  58.                 ],
  59.             ];
  60.             $result $repo->create([$attributeSet], Context::createDefaultContext());
  61.         }
  62.     }
  63.     public function uninstall(UninstallContext $context): void
  64.     {
  65.         if(!$context->keepUserData()){
  66.             $repo $this->container->get('custom_field_set.repository');
  67.             $criteria = new Criteria();
  68.             $criteria->addFilter(new EqualsFilter('name''preorder'));
  69.             $result $repo->search($criteriaContext::createDefaultContext());
  70.             if($result != null){
  71.                 $attributeSet $result->first();
  72.                 if($attributeSet != null){
  73.                     $id $attributeSet->getId();
  74.                     if($id != null){
  75.                         $repo->delete([['id' => $id]], Context::createDefaultContext());
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }