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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Huebert\CrossSellingVariants;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  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\Framework\Uuid\Uuid;
  14. use Shopware\Core\System\CustomField\CustomFieldTypes;
  15. class HuebertCrossSellingVariants extends Plugin
  16. {
  17.     public function install(InstallContext $installContext): void
  18.     {
  19.         parent::install($installContext); // TODO: Change the autogenerated stub
  20.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  21.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  22.         $customFieldSet = [
  23.             'name' => 'huebert_cross_selling_variants',
  24.             'config' => [
  25.                 'label' => [
  26.                     'en-GB' => 'Csutom field set fo Huebert Cross Selling Variants plugin',
  27.                     'de-DE' => 'Custom Field set für Hübert Cross Selling Variants Plugin'
  28.                 ]
  29.             ],
  30.             'customFields' => [
  31.                 [
  32.                     'name' => 'huebert_cross_selling_variants_display_element',
  33.                     'type' => CustomFieldTypes::SWITCH,
  34.                     'config' => [
  35.                         'label' => [
  36.                             'en-GB' => 'Hide Cross-Selling Element on detail page.',
  37.                             'de-DE' => 'Cross-Selling Element auf Detail Page ausblenden.'
  38.                         ],
  39.                         'customFieldPosition' => 1
  40.                     ]
  41.                 ]
  42.             ],
  43.             'relations' => [
  44.                 [
  45.                     'entityName' => 'product'
  46.                 ]
  47.             ]
  48.         ];
  49.         $customFieldSetRepository->create([$customFieldSet], $installContext->getContext());
  50.     }
  51.     public function uninstall(UninstallContext $uninstallContext): void
  52.     {
  53.         parent::uninstall($uninstallContext);
  54.         if ($uninstallContext->keepUserData()) {
  55.             return;
  56.         }
  57.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  58.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  59.         $criteria = (new Criteria())
  60.             ->addFilter(new EqualsFilter('name''huebert_cross_selling_variants'));
  61.         $customFieldSetId $customFieldSetRepository->searchIds($criteria$uninstallContext->getContext())->firstId();
  62.         $customFieldSetRepository->delete([
  63.             [
  64.                 'id' => $customFieldSetId
  65.             ]
  66.         ], $uninstallContext->getContext());
  67.         /** @var EntityRepositoryInterface $productRepository */
  68.         $productRepository $this->container->get('product.repository');
  69.         /** @var EntityRepositoryInterface $crossSellingRepository */
  70.         $crossSellingRepository $this->container->get('product_cross_selling.repository');
  71.         $criteria = (new Criteria())
  72.             ->addAssociation('crossSellings')
  73.             ->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND, [
  74.                 new NotFilter(MultiFilter::CONNECTION_AND, [
  75.                     new EqualsFilter('parent.id'null)
  76.                 ])
  77.             ]));
  78.         $products $productRepository->search($criteria$uninstallContext->getContext());
  79.         /** @var ProductEntity $product */
  80.         foreach ($products->getElements() as $product) {
  81.             if ($product->getCrossSellings()->count() > 0) {
  82.                 $crossSellings $product->getCrossSellings()->getIds();
  83.                 $deleteData = [];
  84.                 foreach ($crossSellings as $crossSellingId) {
  85.                     $deleteData[] = [
  86.                         'id' => $crossSellingId
  87.                     ];
  88.                 }
  89.                 $crossSellingRepository->delete($deleteData$uninstallContext->getContext());
  90.             }
  91.         }
  92.     }
  93. }