<?php
namespace CioCustomOrdernumber\Subscriber;
use CioExports\Service\Order\PositionExporter;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Event\OrderNumberAssignEvent;
use Symfony\Component\HttpFoundation\RequestStack;
class CioCustomOrdernumberSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(
RequestStack $requestStack,
) {
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'onChangeOrderNumber'
];
}
public function onChangeOrderNumber(CartConvertedEvent $event): void
{
// WS/PERSOID/KOSTENSTELLENNR/Bestellnummer
$prefix = 'WS-';
$prefix .= $event->getSalesChannelContext()->getCustomer()->getCustomerNumber() . '-';
if ($costsCenterNb = $this->requestStack->getCurrentRequest()->request->get('cio_costcenter')) {
$prefix .= $costsCenterNb . '-';
}
$convertedCart = $event->getConvertedCart();
$convertedCart['orderNumber'] = $prefix . $convertedCart['orderNumber'];
$event->setConvertedCart($convertedCart);
}
}