custom/plugins/TdsMerwareDiscounts/src/TdsMerwareDiscounts.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tds\MerwareDiscounts;
  3. use Shopware\Core\Checkout\Customer\CustomerDefinition;
  4. use Shopware\Core\Content\Product\ProductDefinition;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\System\CustomField\CustomFieldTypes;
  13. class TdsMerwareDiscounts extends Plugin
  14. {
  15.     public function install(InstallContext $installContext): void
  16.     {
  17.         /**
  18.          * @var EntityRepositoryInterface $customFieldSetRepository
  19.          */
  20.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  21.         $customFieldSetRepository->create($this->getCustomFieldSets(), $installContext->getContext());
  22.     }
  23.     /**
  24.      * {@inheritDoc}
  25.      *
  26.      * @param UninstallContext $uninstallContext
  27.      *
  28.      * @throws InconsistentCriteriaIdsException
  29.      */
  30.     public function uninstall(UninstallContext $uninstallContext): void
  31.     {
  32.         $this->deleteCustomFieldSet($this->getCustomFieldSets(), $uninstallContext->getContext());
  33.         $this->deleteCustomFields($this->getCustomFieldSets(), $uninstallContext->getContext());
  34.     }
  35.     /**
  36.      * Helper-function to trigger the deletion of plugin custom field sets
  37.      *
  38.      * @param $customFieldSets
  39.      * @param $context
  40.      * @throws InconsistentCriteriaIdsException
  41.      */
  42.     private function deleteCustomFieldSet($customFieldSets$context): void
  43.     {
  44.         /**
  45.          * @var EntityRepositoryInterface $customFieldSetRepository
  46.          */
  47.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  48.         $ids $this->getCustomFieldSetIds($customFieldSets$context);
  49.         if (!empty($ids)) {
  50.             $customFieldSetRepository->delete($ids$context);
  51.         }
  52.     }
  53.     /**
  54.      * Helper-function to get custom field set ids
  55.      *
  56.      * @param $customFieldSets
  57.      * @param $context
  58.      *
  59.      * @return array
  60.      *
  61.      * @throws InconsistentCriteriaIdsException
  62.      */
  63.     private function getCustomFieldSetIds($customFieldSets$context)
  64.     {
  65.         /**
  66.          * @var EntityRepositoryInterface $customFieldSetRepository
  67.          */
  68.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  69.         $ids = [];
  70.         foreach ($customFieldSets as $customFieldSet) {
  71.             $criteria = new Criteria();
  72.             $criteria->addFilter(
  73.                 new EqualsFilter('name'$customFieldSet['name'])
  74.             );
  75.             $customFieldSetId $customFieldSetRepository->searchIds($criteria$context)->firstId();
  76.             if (!empty($customFieldSetId)) {
  77.                 $ids[] = [
  78.                     'id' => $customFieldSetId
  79.                 ];
  80.             }
  81.         }
  82.         return $ids;
  83.     }
  84.     /**
  85.      * Helper-function to delete custom fields
  86.      *
  87.      * @param $customFieldSets
  88.      * @param $context
  89.      *
  90.      * @throws InconsistentCriteriaIdsException
  91.      */
  92.     private function deleteCustomFields($customFieldSets$context): void
  93.     {
  94.         /**
  95.          * @var EntityRepositoryInterface $customFieldRepository
  96.          */
  97.         $customFieldRepository $this->container->get('custom_field.repository');
  98.         $ids $this->getCustomFieldIds($customFieldSets$context);
  99.         if (!empty($ids)) {
  100.             $customFieldRepository->delete($ids$context);
  101.         }
  102.     }
  103.     /**
  104.      * Helper-function to get a list of custom field ids
  105.      *
  106.      * @param $customFieldSets
  107.      * @param $context
  108.      *
  109.      * @return array
  110.      *
  111.      * @throws InconsistentCriteriaIdsException
  112.      */
  113.     private function getCustomFieldIds($customFieldSets$context)
  114.     {
  115.         /**
  116.          * @var EntityRepositoryInterface $customFieldRepository
  117.          */
  118.         $customFieldRepository $this->container->get('custom_field.repository');
  119.         $ids = [];
  120.         foreach ($customFieldSets as $customFieldSet) {
  121.             if (!empty($customFieldSet['customFields'])) {
  122.                 foreach ($customFieldSet['customFields'] as $customField) {
  123.                     $criteria = new Criteria();
  124.                     $criteria->addFilter(
  125.                         new EqualsFilter('name'$customField['name'])
  126.                     );
  127.                     $customFieldId $customFieldRepository->searchIds($criteria$context)->firstId();
  128.                     if (!empty($customFieldId)) {
  129.                         $ids[] = [
  130.                             'id' => $customFieldId
  131.                         ];
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.         return $ids;
  137.     }
  138.     private function getCustomFieldSets()
  139.     {
  140.         return [
  141.             [
  142.                 'name' => 'tds_merware_product_discount',
  143.                 'config' => [
  144.                     'label' => [
  145.                         'de-DE' => 'Rabatt',
  146.                         'en-GB' => 'Discount',
  147.                     ]
  148.                 ],
  149.                 'customFields' => [
  150.                     [
  151.                         'name' => 'tds_merware_product_discount_activate',
  152.                         'type' => 'bool',
  153.                         'config' => [
  154.                             'customFieldPosition' => 1,
  155.                             'numberType' => 'integer',
  156.                             'label' => [
  157.                                 'de-DE' => 'Rabattfähig',
  158.                                 'en-GB' => 'Discountable',
  159.                             ],
  160.                             'helpText' => [
  161.                                 'de-DE' => null,
  162.                                 'en-GB' => null,
  163.                             ],
  164.                             'componentName' => 'sw-field',
  165.                             'customFieldType' => 'checkbox',
  166.                         ]
  167.                     ]
  168.                 ],
  169.                 'relations' => [
  170.                     [
  171.                         'entityName' => $this->container->get(ProductDefinition::class)->getEntityName()
  172.                     ]
  173.                 ]
  174.             ],
  175.             [
  176.                 'name' => 'tds_merware_customer_discount',
  177.                 'config' => [
  178.                     'label' => [
  179.                         'de-DE' => 'Rabatte',
  180.                         'en-GB' => 'Discounts',
  181.                     ],
  182.                     'translated' => false,
  183.                 ],
  184.                 'customFields' => [
  185.                     [
  186.                         'name' => 'tds_merware_customer_discount_general',
  187.                         'type' => 'number',
  188.                         'config' => [
  189.                             'customFieldPosition' => 1,
  190.                             'numberType' => 'float',
  191.                             'label' => [
  192.                                 'de-DE' => 'Generalrabatt',
  193.                                 'en-GB' => 'General discount',
  194.                             ],
  195.                             'componentName' => 'sw-field',
  196.                             'customFieldType' => 'number',
  197.                         ]
  198.                     ],
  199.                     [
  200.                         'name' => 'tds_merware_customer_discount_products',
  201.                         'type' => 'text',
  202.                         'config' => [
  203.                             'customFieldPosition' => 2,
  204.                             'label' => [
  205.                                 'de-DE' => 'Sonderpreise',
  206.                                 'en-GB' => 'Special prices',
  207.                             ],
  208.                             'componentName' => 'sw-field',
  209.                             'customFieldType' => 'text',
  210.                         ]
  211.                     ]
  212.                 ],
  213.                 'relations' => [
  214.                     [
  215.                         'entityName' => $this->container->get(CustomerDefinition::class)->getEntityName()
  216.                     ]
  217.                 ]
  218.             ]
  219.         ];
  220.     }
  221. }