<?php declare(strict_types=1);
namespace Shopware\Commercial\MultiWarehouse\Subscriber;
use Doctrine\DBAL\Connection;
use Shopware\Commercial\Licensing\License;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @internal
*
* @package inventory
*/
class CartConvertedSubscriber implements EventSubscriberInterface
{
private Connection $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'onCartConverted',
];
}
public function onCartConverted(CartConvertedEvent $event): void
{
if (!License::get('MULTI_INVENTORY-3749997')) {
return;
}
if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
return;
}
$ids = $this->getAvailableWarehouseGroups($event->getSalesChannelContext());
if (empty($ids)) {
return;
}
$cartData = $event->getConvertedCart();
$cartData['warehouseGroups'] = \array_map(
static fn (string $id) => ['warehouseGroupId' => $id],
$ids
);
$event->setConvertedCart($cartData);
}
/**
* @return list<string>
*/
private function getAvailableWarehouseGroups(SalesChannelContext $context): array
{
/** @var list<string> $groups */
$groups = $this->connection->fetchFirstColumn(
'SELECT LOWER(HEX(warehouse_group.id)) FROM warehouse_group WHERE warehouse_group.rule_id IN (:ids)',
['ids' => Uuid::fromHexToBytesList($context->getRuleIds())],
['ids' => Connection::PARAM_STR_ARRAY]
);
return $groups;
}
}