custom/static-plugins/NdTheme/src/Subscriber/AddProductLabels.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Theme\Subscriber;
  3. use DateTime;
  4. use Nd\Coef\Service\ProductManufacturerUrlService;
  5. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Content\Product\SalesChannel\Detail\ProductConfiguratorLoader;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Struct\ArrayStruct;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Shopware\Core\Content\Product\ProductEvents;
  19. class AddProductLabels implements EventSubscriberInterface
  20. {
  21.     private EventDispatcherInterface $dispatcher;
  22.     private string $labelPropertyId;
  23.     private string $labelFitId;
  24.     private int $labelMaxDaysNewProduct;
  25.     private EntityRepository $productManufacturerRepository;
  26.     private EntityRepository $productRepository;
  27.     private EntityRepository $propertyGroupOptionRepository;
  28.     private array $results;
  29.     public function __constructEventDispatcherInterface $dispatcher,
  30.                                  EntityRepository $productManufacturerRepositoryEntityRepository $productRepository,
  31.                                 EntityRepository $propertyGroupOptionRepository,
  32.                                 string $labelPropertyIdstring $labelFitIdint $labelMaxDaysNewProduct)
  33.     {
  34.         $this->dispatcher $dispatcher;
  35.         $this->labelPropertyId $labelPropertyId;
  36.         $this->labelFitId $labelFitId;
  37.         $this->labelMaxDaysNewProduct $labelMaxDaysNewProduct;
  38.         $this->productManufacturerRepository $productManufacturerRepository;
  39.         $this->productRepository $productRepository;
  40.         $this->propertyGroupOptionRepository $propertyGroupOptionRepository;
  41.         $this->results = [];
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
  47.         ];
  48.     }
  49.     public function onProductsLoaded(EntityLoadedEvent $event): void
  50.     {
  51. //        $this->dispatcher->removeSubscriber($this);
  52.         /** @var ProductEntity $productEntity */
  53.         foreach ($event->getEntities() as $productEntity) {
  54.             if(in_array($productEntity->getId(), array_keys($this->results))){
  55.                 $label $this->results[$productEntity->getId()]['label'];
  56.                 $fit $this->results[$productEntity->getId()]['fit'];
  57.             }else{
  58.                 $label $this->getLabel($productEntity$event->getContext());
  59.                 $fit $this->getFit($productEntity$event->getContext());
  60.                 $this->results[$productEntity->getId()] = [
  61.                     'label' => $label,
  62.                     'fit' => $fit
  63.                 ];
  64.             }
  65.             if ($label) {
  66.                 $productEntity->addExtension('labelData', new ArrayStruct($label));
  67.             }
  68.             if ($fit) {
  69.                 $productEntity->addExtension('fitName', new ArrayStruct([$fit]));
  70.             }
  71.             if (!$productEntity->getManufacturer() && $productEntity->getManufacturerId()) {
  72.                 $criteria = new Criteria([$productEntity->getManufacturerId()]);
  73.                 $manufacturers $this->productManufacturerRepository->search($criteria$event->getContext());
  74.                 if ($manufacturers->count() > 0) {
  75.                     $productEntity->setManufacturer($manufacturers->first());
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     public function getFit(ProductEntity $productContext $context)
  81.     {
  82.         $sizeFitId $this->labelFitId;
  83.         if(!$sizeFitId || $sizeFitId == "undefined"){
  84.             return false;
  85.         }
  86.         if($product->getProperties()){
  87.             $fits $product->getProperties()->filterByGroupId($sizeFitId);
  88.         } else {
  89.             $criteria = new Criteria();
  90.             $criteria->addFilter(new EqualsFilter('productProperties.id'$product->getId()));
  91.             $criteria->addFilter(new EqualsFilter('productProperties.versionId'$product->getVersionId()));
  92.             $criteria->addFilter(new EqualsFilter('groupId'$sizeFitId));
  93.             $fits $this->propertyGroupOptionRepository->search($criteria$context);
  94.         }
  95.         if ($fits->count() === 0) {
  96.             return false;
  97.         }
  98.         return $fits->first()->getName();
  99.     }
  100.     private function getProductProperties(ProductEntity $productContext $context){
  101.         $properties $product->getProperties();
  102.         if (! $properties) {
  103.             $criteria = new Criteria([$product->getId()]);
  104.             $criteria->addAssociation("properties.group");
  105.             $productSearch $this->productRepository->search($criteria$context);
  106.             if($productSearch->count() != 1) {
  107.                 return false;
  108.             }
  109.             $product->setProperties($productSearch->first()->getProperties());
  110.             $properties $product->getProperties();
  111.         }
  112.         return $properties;
  113.     }
  114.     public function getLabel(ProductEntity $productContext $context)
  115.     {
  116.         $price $product->getCurrencyPrice($context->getCurrencyId());
  117.         if ($price && $price->getPercentage() && $price->getPercentage()["net"] > 0) {
  118.             return ['label' => 'sale''text' => "SALE"];
  119.         }
  120.         if (!$this->labelPropertyId || $this->labelPropertyId == "undefined") {
  121.             return false;
  122.         }
  123.         if(!$product->getProperties()){
  124.             $criteria = new Criteria();
  125.             $criteria->addFilter(new EqualsFilter('productProperties.id'$product->getId()));
  126.             $criteria->addFilter(new EqualsFilter('productProperties.versionId'$product->getVersionId()));
  127.             $criteria->addFilter(new EqualsFilter('groupId'$this->labelPropertyId));
  128.             $labelProperties $this->propertyGroupOptionRepository->search($criteria$context);
  129.         }else{
  130.             $labelProperties $product->getProperties()->filterByGroupId($this->labelPropertyId);
  131.         }
  132.         if ($labelProperties->count() > 0) {
  133.             $highestProperty null;
  134.             $highest = -1;
  135.             foreach ($labelProperties as $labelProperty) {
  136.                 if ($labelProperty->getPosition() > $highest) {
  137.                     $highestProperty $labelProperty;
  138.                     $highest $labelProperty->getPosition();
  139.                 }
  140.             }
  141.             if ($highestProperty) {
  142.                 return ['label' => 'property''text' => $highestProperty->getName(), 'color' => $highestProperty->getColorHexCode()];
  143.             } else {
  144.                 return false;
  145.             }
  146.         }
  147.         $now = new DateTime();
  148.         if ($product->getCreatedAt()->diff($now)->format('%a') < $this->labelMaxDaysNewProduct) {
  149.             return ['label' => 'NEW''text' => "Nieuw"];
  150.         }
  151.         return false;
  152.     }
  153. }