custom/plugins/AcrisSuppliesUpsellingCS/src/Storefront/Subscriber/ProductQuantitySubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\SuppliesUpselling\Storefront\Subscriber;
  3. use Shopware\Core\Content\Product\Aggregate\ProductCrossSelling\ProductCrossSellingEntity;
  4. use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
  5. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CrossSellingElement;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. class ProductQuantitySubscriber implements EventSubscriberInterface
  13. {
  14.     const DEFAULT_CROSS_SELLING_PRODUCT_LIST_TYPE 'productList';
  15.     /**
  16.      * @var SystemConfigService
  17.      */
  18.     private $systemConfigService;
  19.     /**
  20.      * @var EntityRepositoryInterface
  21.      */
  22.     private $repository;
  23.     public function __construct(SystemConfigService $systemConfigServiceEntityRepositoryInterface $repository)
  24.     {
  25.         $this->systemConfigService $systemConfigService;
  26.         $this->repository $repository;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductCrossSellingsLoadedEvent::class => 'onCrossSellingLoaded',
  32.         ];
  33.     }
  34.     public function onCrossSellingLoaded(ProductCrossSellingsLoadedEvent $event)
  35.     {
  36.         $crossSellings $event->getCrossSellings()->getElements();
  37.         $productStreamArray = [];
  38.         $customQuantityArray = [];
  39.         $assignedProducts = [];
  40.         if ($crossSellings) {
  41.             /** @var CrossSellingElement $crossSelling */
  42.             foreach ($crossSellings as $crossSelling) {
  43.                 if ($crossSelling->getCrossSelling()->getType() === self::DEFAULT_CROSS_SELLING_PRODUCT_LIST_TYPE) {
  44.                     if (!empty($crossSelling->getCrossSelling()) && !empty($crossSelling->getCrossSelling()->getAssignedProducts()) && $crossSelling->getCrossSelling()->getAssignedProducts()->count() > 0) {
  45.                         foreach ($crossSelling->getCrossSelling()->getAssignedProducts()->getElements() as $assignedProduct) {
  46.                             if (!empty($assignedProduct) && !empty($assignedProduct->getExtensions()) && $assignedProduct->hasExtension('acrisProductCrossSellingAssignedProduct') && !empty($assignedProduct->getExtension('acrisProductCrossSellingAssignedProduct')) && !empty($assignedProduct->getExtension('acrisProductCrossSellingAssignedProduct')->getQuantity())) {
  47.                                 $customQuantityArray[$assignedProduct->getProductId()] = $assignedProduct->getExtension('acrisProductCrossSellingAssignedProduct')->getQuantity();
  48.                             } else {
  49.                                 $customQuantityArray[$assignedProduct->getProductId()] = 1;
  50.                             }
  51.                         }
  52.                     }
  53.                 } else {
  54.                     $productStreamId $crossSelling->getCrossSelling()->getProductStreamId();
  55.                     if ($productStreamId) {
  56.                         $productStreamFilterRepository $this->repository->search((new Criteria())->addFilter(new EqualsFilter('productStreamId'$productStreamId))->addFilter(new EqualsFilter('field''productNumber'))->addFilter(new EqualsFilter('type''equals')), $event->getContext());
  57.                         if ($productStreamFilterRepository){
  58.                             foreach($productStreamFilterRepository as $productStream){
  59.                                 if($productStream){
  60.                                     $productStreamArray[] = $productStream;
  61.                                 }
  62.                             }
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         if($productStreamArray){
  69.             foreach ($productStreamArray as $product) {
  70.                 if (!empty($product->getCustomFields())) {
  71.                     if ($product->getCustomFields()["acris_accessory_quantity"] != 0) {
  72.                         $customQuantityArray[$product->getValue()] = $product->getCustomFields()["acris_accessory_quantity"];
  73.                     } else {
  74.                         $customQuantityArray[$product->getValue()] = 1;
  75.                     }
  76.                 } else {
  77.                     $customQuantityArray[$product->getValue()] = 1;
  78.                 }
  79.             }
  80.         }
  81.         if (!empty($customQuantityArray)) {
  82.             $event->getCrossSellings()->addExtension('acris_accessory_quantity', new ArrayEntity([
  83.                 $customQuantityArray
  84.             ]));
  85.         }
  86.     }
  87. }