custom/plugins/CioFixDuplicateDiscounts/src/Subscriber/FixDuplicateDiscounts.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CioFixDuplicateDiscounts\Subscriber;
  3. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  6. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  8. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class FixDuplicateDiscounts implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             OffcanvasCartPageLoadedEvent::class => 'onCartEvents',
  18.             CheckoutCartPageLoadedEvent::class => 'onCartEvents',
  19.             CheckoutConfirmPageLoadedEvent::class => 'onCartEvents',
  20.             CheckoutFinishPageLoadedEvent::class => 'onOrderEvents',
  21.             AccountOrderPageLoadedEvent::class => 'onOrderEvents'
  22.         ];
  23.     }
  24.     /**
  25.      * @param OffcanvasCartPageLoadedEvent|CheckoutCartPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event
  26.      * @return void
  27.      */
  28.     public function onCartEvents($event)
  29.     {
  30.         $this->removeDuplicateDiscount($event->getPage()->getCart()->getLineItems());
  31.     }
  32.     /**
  33.      * @param CheckoutFinishPageLoadedEvent|AccountOrderPageLoadedEvent $event
  34.      * @return void
  35.      */
  36.     public function onOrderEvents($event)
  37.     {
  38.         if ($event instanceof AccountOrderPageLoadedEvent) {
  39.             foreach ($event->getPage()->getOrders() as $order) {
  40.                 $this->removeDuplicateDiscount($order->getLineItems());
  41.             }
  42.         } else {
  43.             $this->removeDuplicateDiscount($event->getPage()->getOrder()->getLineItems());
  44.         }
  45.     }
  46.     /**
  47.      * @param LineItemCollection|OrderLineItemCollection $lineItemCollection
  48.      * @return void
  49.      */
  50.     public function removeDuplicateDiscount($lineItemCollection)
  51.     {
  52.         $voucherItems = [];
  53.         $labels = [];
  54.         foreach ($lineItemCollection as $lineItem) {
  55.             if ($lineItem->getType() == LineItem::PROMOTION_LINE_ITEM_TYPE) {
  56.                 $voucherItems[] =
  57.                     [
  58.                         'id' => $lineItem->getId(),
  59.                         'label' => $lineItem->getLabel(),
  60.                         'price' => $lineItem->getPrice()->getUnitPrice()
  61.                     ];
  62.                 $labels[] = $lineItem->getLabel();
  63.             }
  64.         }
  65.         $idsToRemove = [];
  66.         foreach ($voucherItems as $voucherItem) {
  67.             if ($voucherItem['price'] == 0) {
  68.                 if (in_array($voucherItem['label'], $labels))
  69.                     $idsToRemove[] = $voucherItem['id'];
  70.             }
  71.         }
  72.         foreach ($idsToRemove as $id) {
  73.             $lineItemCollection->remove($id);
  74.         }
  75.     }
  76. }