custom/plugins/AcrisDiscountGroupCS/src/Storefront/Subscriber/DiscountGroupSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\DiscountGroup\Storefront\Subscriber;
  3. use Acris\DiscountGroup\Components\DiscountGroupGateway;
  4. use Acris\DiscountGroup\Components\DiscountGroupService;
  5. use Acris\DiscountGroup\Components\Struct\LineItemDiscountGroupData;
  6. use Acris\DiscountGroup\Custom\DiscountGroupEntity;
  7. use Shopware\Core\Checkout\Cart\Cart;
  8. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  9. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  10. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  11. use Shopware\Core\Framework\Struct\ArrayEntity;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class DiscountGroupSubscriber implements EventSubscriberInterface
  17. {
  18.     private CartService $cartService;
  19.     private DiscountGroupGateway $discountGroupGateway;
  20.     private SystemConfigService $systemConfigService;
  21.     public function __construct(
  22.         CartService $cartService,
  23.         DiscountGroupGateway $discountGroupGateway,
  24.         SystemConfigService $systemConfigService
  25.     ) {
  26.         $this->cartService $cartService;
  27.         $this->discountGroupGateway $discountGroupGateway;
  28.         $this->systemConfigService $systemConfigService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  34.         ];
  35.     }
  36.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  37.     {
  38.         $salesChannelContext $event->getSalesChannelContext();
  39.         if ($this->systemConfigService->get('AcrisDiscountGroupCS.config.activateAcrossScaleDiscounts'$salesChannelContext->getSalesChannel()->getId()) !== true || $this->systemConfigService->get('AcrisDiscountGroupCS.config.showCartQuantityInfoDetail'$salesChannelContext->getSalesChannel()->getId()) !== 'aboveScalePrices') {
  40.             return;
  41.         }
  42.         $product $event->getPage()->getProduct();
  43.         $cart $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
  44.         $lineItem $cart->getLineItems()->filter(function(LineItem $item) use ($product) {
  45.            return $item->getReferencedId() === $product->getId();
  46.         })->first();
  47.         if (empty($lineItem) || !$lineItem instanceof LineItem) {
  48.             $lineItem $this->loadLineItemWithSameDiscountGroup($cart$product$salesChannelContext);
  49.         }
  50.         if (empty($lineItem) || !$lineItem instanceof LineItem) {
  51.             return;
  52.         }
  53.         list($id$name$quantity) = $this->loadDiscountGroupDataFromPayload($lineItem->getPayload());
  54.         if (!is_int($quantity)) {
  55.             return;
  56.         }
  57.         $discountGroupData = new LineItemDiscountGroupData($id$name$quantity);
  58.         $product->addExtension('acrisDiscountGroupLineItemData'$discountGroupData);
  59.     }
  60.     private function loadDiscountGroupDataFromPayload(?array $payload): array
  61.     {
  62.         $id null;
  63.         $name null;
  64.         $quantity null;
  65.         if (empty($payload) || !is_array($payload)) {
  66.             return [$id$name$quantity];
  67.         }
  68.         if (array_key_exists('discountGroupScalePriceId'$payload)) {
  69.             $id $payload['discountGroupScalePriceId'];
  70.         }
  71.         if (array_key_exists('discountGroupScalePriceDisplayName'$payload)) {
  72.             $name $payload['discountGroupScalePriceDisplayName'];
  73.         }
  74.         if (array_key_exists('discountGroupScalePriceQuantity'$payload)) {
  75.             $quantity $payload['discountGroupScalePriceQuantity'];
  76.         }
  77.         return [$id$name$quantity];
  78.     }
  79.     private function loadLineItemWithSameDiscountGroup(Cart $cartSalesChannelProductEntity $productSalesChannelContext $salesChannelContext): ?LineItem
  80.     {
  81.         if ($cart->getLineItems()->count() === 0) {
  82.             return null;
  83.         }
  84.         if ($product->hasExtension(DiscountGroupService::ACRIS_STREAM_IDS_EXTENSION) && !empty($product->getExtension(DiscountGroupService::ACRIS_STREAM_IDS_EXTENSION))) {
  85.             $productStreamIds $product->getExtension(DiscountGroupService::ACRIS_STREAM_IDS_EXTENSION)->get('ids');
  86.         } else {
  87.             $productStreamIds $this->discountGroupGateway->getProductStreamIds([$product->getId()], $salesChannelContext->getContext());
  88.             $product->addExtension(DiscountGroupService::ACRIS_STREAM_IDS_EXTENSION, new ArrayEntity(['ids' => $productStreamIds]));
  89.         }
  90.         $discountGroupResult $this->discountGroupGateway->getAllDiscountGroupsForProduct($salesChannelContext$product->getId(), $productStreamIds);
  91.         if ($discountGroupResult->getTotal() === 0) {
  92.             return null;
  93.         }
  94.         /** @var DiscountGroupEntity $discountGroup */
  95.         foreach ($discountGroupResult->getEntities()->getElements() as $discountGroup) {
  96.             $lineItem $cart->getLineItems()->filter(function(LineItem $item) use ($discountGroup) {
  97.                 return $item->hasPayloadValue('discountGroupScalePriceId') && !empty($item->getPayloadValue('discountGroupScalePriceId')) && $item->getPayloadValue('discountGroupScalePriceId') === $discountGroup->getId();
  98.             })->first();
  99.             if (!empty($lineItem) && $lineItem instanceof LineItem) {
  100.                 return $lineItem;
  101.             }
  102.         }
  103.         return null;
  104.     }
  105. }