custom/plugins/SelfdelveBillbee/src/SelfdelveBillbee.php line 23

Open in your IDE?
  1. <?php declare(strict_types=1);
  2.     namespace SelfdelveBillbee;
  3.     use Shopware\Core\Framework\Plugin;
  4.     use Shopware\Core\Framework\Plugin\Context\InstallContext;
  5.     use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  6.     use Shopware\Core\System\CustomField\CustomFieldTypes;
  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.     use Shopware\Core\Framework\Context;
  11.     // Manually load package classes as it is not possible in the Plugin's composer file yet
  12.     if (file_exists(\dirname(__DIR__) . '/packages/billbee-php-sdk/vendor/autoload.php')) {
  13.         $loader = require_once \dirname(__DIR__) . '/packages/billbee-php-sdk/vendor/autoload.php';
  14.         if ($loader !== true) {
  15.             spl_autoload_unregister([$loader'loadClass']);
  16.             $loader->register(false);
  17.         }
  18.     }
  19.     class SelfdelveBillbee extends Plugin
  20.     {
  21.         const CUSTOM_FIELDS_SET_SHIPPING_METHODS   'selfdelve_billbee_shipping_methods';
  22.         /**
  23.          * @param InstallContext $installContext
  24.          */
  25.         public function install(InstallContext $installContext): void
  26.         {
  27.             $this->addCustomFieldSet($installContext->getContext());
  28.         }
  29.         /**
  30.          * @param UninstallContext $uninstallContext
  31.          */
  32.         public function uninstall(UninstallContext $uninstallContext): void
  33.         {
  34.             parent::uninstall($uninstallContext);
  35.             if ($uninstallContext->keepUserData()) {
  36.                 return;
  37.             }
  38.             $this->removeCustomFieldSet($uninstallContext->getContext());
  39.         }
  40.         /**
  41.          * @param Context $context
  42.          */
  43.         private function addCustomFieldSet(Context $context): void
  44.         {
  45.             $customFieldSetRepo $this->container->get('custom_field_set.repository');
  46.             if ($customFieldSetRepo->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELDS_SET_SHIPPING_METHODS)), $context)->count() === 0) {
  47.                 $customFieldSetRepo->create([
  48.                     [
  49.                         'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHODS,
  50.                         'position' => 100,
  51.                         'config' => [
  52.                             'label' => [
  53.                                 'en-GB' => 'SelfDelve - Shipping Methods',
  54.                                 'de-DE' => 'SelfDelve - Versandarten'
  55.                             ]
  56.                         ],
  57.                         'customFields' => [
  58.                             [
  59.                                 'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHODS '_name_billbee',
  60.                                 'type' => CustomFieldTypes::TEXT,
  61.                                 'config' => [
  62.                                     'label' => [
  63.                                         'en-GB' => 'Shipping product name in Billbee',
  64.                                         'de-DE' => 'Name des Versandprodukts in Billbee'
  65.                                     ],
  66.                                     'customFieldType' => 'text',
  67.                                     'customFieldPosition' => 0
  68.                                 ]
  69.                             ],
  70.                         ],
  71.                         'relations' => [
  72.                             [
  73.                                 'id' => Uuid::randomHex(),
  74.                                 'entityName' => 'shipping_method'
  75.                             ]
  76.                         ]
  77.                     ]
  78.                 ], $context);
  79.             }
  80.         }
  81.         /**
  82.          * @param Context $context
  83.          */
  84.         private function removeCustomFieldSet(Context $context): void
  85.         {
  86.             $customFieldSetRepo $this->container->get('custom_field_set.repository');
  87.             $id $customFieldSetRepo->searchIds((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELDS_SET_SHIPPING_METHODS)), $context)->firstId();
  88.             if ($id) {
  89.                 $customFieldSetRepo->delete([
  90.                     ['id' => $id]
  91.                 ], $context);
  92.             }
  93.         }
  94.     }