custom/plugins/KplngiProductOrder/src/Subscriber/ProductCategoryChange.php line 64

Open in your IDE?
  1. <?php
  2. namespace Kplngi\ProductOrder\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ProductCategoryChange implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var EntityRepositoryInterface
  13.      */
  14.     private $productCategoryPositionRepository;
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private $orderActiveRepository;
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $productRepository;
  23.     public function __construct(
  24.         EntityRepositoryInterface $entityRepository,
  25.         EntityRepositoryInterface $orderActiveRepository,
  26.         EntityRepositoryInterface $productRepository)
  27.     {
  28.         $this->productCategoryPositionRepository $entityRepository;
  29.         $this->orderActiveRepository $orderActiveRepository;
  30.         $this->productRepository $productRepository;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             'product_category.written' => 'onAssociationWritten',
  36.             'product_category.deleted' => 'onAssociationDeleted'
  37.         ];
  38.     }
  39.     public function onAssociationDeleted(EntityDeletedEvent $event)
  40.     {
  41.         foreach ($event->getIds() as $productCategoryDeleted) {
  42.             $criteria = new Criteria();
  43.             $criteria->addFilter(new EqualsFilter('productId'$productCategoryDeleted['productId']));
  44.             $criteria->addFilter(new EqualsFilter('categoryId'$productCategoryDeleted['categoryId']));
  45.             $positionsToDelete $this->productCategoryPositionRepository->searchIds($criteria$event->getContext());
  46.             foreach ($positionsToDelete->getIds() as $positionIdToDelete) {
  47.                 $this->productCategoryPositionRepository->delete([
  48.                     ['id' => $positionIdToDelete]
  49.                 ], $event->getContext());
  50.             }
  51.         }
  52.     }
  53.     public function onAssociationWritten(EntityWrittenEvent $event)
  54.     {
  55.         foreach ($event->getIds() as $productCategoryAdded) {
  56.             $productCriteria = new Criteria();
  57.             $productCriteria->addFilter(new EqualsFilter('id'$productCategoryAdded['productId']));
  58.             $parentId $this->productRepository->search($productCriteria$event->getContext())->first()->getParentId();
  59.             if ($parentId !== null) {
  60.                 continue;
  61.             }
  62.             $orderActiveCriteria = new Criteria();
  63.             $orderActiveCriteria->addFilter(new EqualsFilter('categoryId'$productCategoryAdded['categoryId']));
  64.             $orderActive $this->orderActiveRepository->search($orderActiveCriteria$event->getContext());
  65.             if ($orderActive->getTotal() === 0) {
  66.                 continue;
  67.             }
  68.             $productOrderedCriteria = new Criteria();
  69.             $productOrderedCriteria->addFilter(new EqualsFilter('productId'$productCategoryAdded['productId']));
  70.             $productOrderedCriteria->addFilter(new EqualsFilter('categoryId'$productCategoryAdded['categoryId']));
  71.             $productOrdered $this->productCategoryPositionRepository->search($productOrderedCriteria$event->getContext());
  72.             if ($productOrdered->getTotal() === 0) {
  73.                 $this->productCategoryPositionRepository->create([
  74.                     [
  75.                         'productId' => $productCategoryAdded['productId'],
  76.                         'categoryId' => $productCategoryAdded['categoryId'],
  77.                         'position' => 0
  78.                     ]
  79.                 ], $event->getContext());
  80.             }
  81.         }
  82.     }
  83. }