custom/static-plugins/checkout/src/WexoCheckout.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\Checkout;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Shopware\Core\System\CustomField\CustomFieldEntity;
  13. /**
  14.  * Class WexoCheckout
  15.  * @package Wexo\Checkout
  16.  */
  17. class WexoCheckout extends Plugin
  18. {
  19.     const CHECKOUT_ADDITIONAL_FIELDS 'checkout_additional_fields';
  20.     /**
  21.      * @param InstallContext $installContext
  22.      */
  23.     public function install(InstallContext $installContext): void
  24.     {
  25.         parent::install($installContext);
  26.         $this->createCustomFieldSet($installContext->getContext());
  27.         $this->createSaleschannelCustomFieldSet($installContext->getContext());
  28.     }
  29.     /**
  30.      * @param UpdateContext $updateContext
  31.      */
  32.     public function update(UpdateContext $updateContext): void
  33.     {
  34.         parent::update($updateContext);
  35.         $this->createCustomFieldSet($updateContext->getContext());
  36.         $this->createSaleschannelCustomFieldSet($updateContext->getContext());
  37.     }
  38.     /**
  39.      * @param UninstallContext $uninstallContext
  40.      */
  41.     public function uninstall(UninstallContext $uninstallContext): void
  42.     {
  43.         parent::uninstall($uninstallContext);
  44.         if (!$uninstallContext->keepUserData()) {
  45.             $context $uninstallContext->getContext();
  46.             /** @var EntityRepositoryInterface $customFieldSetRepository */
  47.             $customFieldSetRepository $this->container->get('custom_field_set.repository');
  48.             $criteria = new Criteria();
  49.             $criteria->addFilter(
  50.                 new EqualsFilter('name'self::CHECKOUT_ADDITIONAL_FIELDS)
  51.             );
  52.             /** @var CustomFieldEntity $first */
  53.             if ($first $customFieldSetRepository->search($criteria$context)->first()) {
  54.                 $customFieldSetRepository->delete(
  55.                     [
  56.                         [
  57.                             'id' => $first->getId()
  58.                         ]
  59.                     ],
  60.                     $context
  61.                 );
  62.             }
  63.         }
  64.     }
  65.     /**
  66.      * @param Context $context
  67.      * @return EntitySearchResult
  68.      */
  69.     private function getSalesChannels(Context $context)
  70.     {
  71.         $salesChannelRepository $this->container->get('sales_channel.repository');
  72.         return $this->container->get('sales_channel.repository')->search(new Criteria(), $context);
  73.     }
  74.     /**
  75.      * @param Context $context
  76.      * @return bool
  77.      */
  78.     private function createSalesChannelCustomFieldSet(Context $context)
  79.     {
  80.         $salesChannels $this->getSalesChannels($context);
  81.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  82.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  83.         if (!$salesChannels->getTotal()) {
  84.             return false;
  85.         }
  86.         foreach ($salesChannels as $channel) {
  87.             $channelId $channel->getId();
  88.             if ($this->fieldExists(
  89.                 self::CHECKOUT_ADDITIONAL_FIELDS '_' $channelId,
  90.                 $customFieldSetRepository,
  91.                 $context
  92.             )
  93.             ) {
  94.                 continue;
  95.             }
  96.             $customFieldSetRepository->create([
  97.                 [
  98.                     'name' => self::CHECKOUT_ADDITIONAL_FIELDS '_' $channelId,
  99.                     'config' => [
  100.                         'label' => [
  101.                             'da-DK' => 'Yderligere checkout felter - ' $channel->getName(),
  102.                             'de-DE' => 'Zusätzliche Checkout-Felder - ' $channel->getName(),
  103.                             'en-GB' => 'Additional checkout fields - ' $channel->getName()
  104.                         ]
  105.                     ],
  106.                     'relations' => [[
  107.                         'entityName' => 'order'
  108.                     ]]
  109.                 ]
  110.             ], $context);
  111.         }
  112.         return true;
  113.     }
  114.     /**
  115.      * @param Context $context
  116.      */
  117.     private function createCustomFieldSet(Context $context)
  118.     {
  119.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  120.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  121.         if ($this->fieldExists(self::CHECKOUT_ADDITIONAL_FIELDS$customFieldSetRepository$context)) {
  122.             return;
  123.         }
  124.         $customFieldSetRepository->create([
  125.             [
  126.                 'name' => self::CHECKOUT_ADDITIONAL_FIELDS,
  127.                 'config' => [
  128.                     'label' => [
  129.                         'da-DK' => 'Yderligere checkout felter',
  130.                         'de-DE' => 'Zusätzliche Checkout-Felder',
  131.                         'en-GB' => 'Additional checkout fields'
  132.                     ]
  133.                 ],
  134.                 'relations' => [[
  135.                     'entityName' => 'order'
  136.                 ]]
  137.             ]
  138.         ], $context);
  139.     }
  140.     /**
  141.      * @param $name
  142.      * @param $repository
  143.      * @return bool
  144.      */
  145.     private function fieldExists($name$repository$context)
  146.     {
  147.         $criteria = new Criteria();
  148.         $criteria->addFilter(
  149.             new EqualsFilter('name'$name)
  150.         );
  151.         if (!$repository->searchIds($criteria$context)->firstId()) {
  152.             return false;
  153.         }
  154.         return true;
  155.     }
  156. }