custom/plugins/GbmedEnev/src/Subscriber/CartConverted.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedEnev
  12.  * @copyright      Copyright (c) 2019, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\Enev\Subscriber;
  18. use Gbmed\Enev\Core\Content\Enev\EnevDefinition;
  19. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  20. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  21. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class CartConverted implements EventSubscriberInterface
  24. {
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             CartConvertedEvent::class => 'processCartConvertedEvent',
  29.         ];
  30.     }
  31.     public function processCartConvertedEvent(CartConvertedEvent $event): void
  32.     {
  33.         $lineItems $event->getCart()->getLineItems();
  34.         $convertedCart $event->getConvertedCart();
  35.         /** @var LineItem $lineItem */
  36.         foreach ($lineItems as $lineItem) {
  37.             $convertedCartLineItemKey $this->findLineItemKeyInConvertedCart($convertedCart$lineItem);
  38.             if ($convertedCartLineItemKey === null
  39.                 || !array_key_exists($convertedCartLineItemKey$convertedCart['lineItems'])
  40.             ) {
  41.                 continue;
  42.             }
  43.             if (!$lineItem->hasExtension(EnevDefinition::propertyName())) {
  44.                 continue;
  45.             }
  46.             /** @var EntityCollection $data */
  47.             $data $lineItem->getExtension(EnevDefinition::propertyName());
  48.             $convertedCart['lineItems'][$convertedCartLineItemKey]['payload']['extensions'][EnevDefinition::propertyName()] = $data;
  49.         }
  50.         $event->setConvertedCart($convertedCart);
  51.     }
  52.     private function findLineItemKeyInConvertedCart(array $convertedCartLineItem $lineItem)
  53.     {
  54.         foreach ($convertedCart['lineItems'] as $key => $convertedCartLineItem) {
  55.             if ($convertedCartLineItem['identifier'] == $lineItem->getId()) {
  56.                 return $key;
  57.             }
  58.         }
  59.         return null;
  60.     }
  61. }