custom/plugins/SwagCommercial/src/MultiWarehouse/Subscriber/CartConvertedSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Commercial\MultiWarehouse\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Commercial\Licensing\License;
  5. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @internal
  12.  *
  13.  * @package inventory
  14.  */
  15. class CartConvertedSubscriber implements EventSubscriberInterface
  16. {
  17.     private Connection $connection;
  18.     public function __construct(Connection $connection)
  19.     {
  20.         $this->connection $connection;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CartConvertedEvent::class => 'onCartConverted',
  26.         ];
  27.     }
  28.     public function onCartConverted(CartConvertedEvent $event): void
  29.     {
  30.         if (!License::get('MULTI_INVENTORY-3749997')) {
  31.             return;
  32.         }
  33.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  34.             return;
  35.         }
  36.         $ids $this->getAvailableWarehouseGroups($event->getSalesChannelContext());
  37.         if (empty($ids)) {
  38.             return;
  39.         }
  40.         $cartData $event->getConvertedCart();
  41.         $cartData['warehouseGroups'] = \array_map(
  42.             static fn (string $id) => ['warehouseGroupId' => $id],
  43.             $ids
  44.         );
  45.         $event->setConvertedCart($cartData);
  46.     }
  47.     /**
  48.      * @return list<string>
  49.      */
  50.     private function getAvailableWarehouseGroups(SalesChannelContext $context): array
  51.     {
  52.         /** @var list<string> $groups */
  53.         $groups $this->connection->fetchFirstColumn(
  54.             'SELECT LOWER(HEX(warehouse_group.id)) FROM warehouse_group WHERE warehouse_group.rule_id IN (:ids)',
  55.             ['ids' => Uuid::fromHexToBytesList($context->getRuleIds())],
  56.             ['ids' => Connection::PARAM_STR_ARRAY]
  57.         );
  58.         return $groups;
  59.     }
  60. }