custom/plugins/WbfkDigitalInvoice64/src/Core/Checkout/CartOrderRouteDecorator.php line 68

Open in your IDE?
  1. <?php
  2. /**
  3.  * WBFK OG
  4.  * Copyright (c) 2024 WBFK OG
  5.  *
  6.  * @copyright  Copyright (c) 2024 WBFK OG (https://wbfk.at)
  7.  * @author     WBFK OG <info@wbfk.at>
  8.  */
  9. declare(strict_types=1);
  10. namespace Wbfk\DigitalInvoice\Core\Checkout;
  11. use Shopware\Core\Checkout\Cart\Cart;
  12. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  13. use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
  14. use Shopware\Core\Checkout\Cart\SalesChannel\AbstractCartOrderRoute;
  15. use Shopware\Core\Checkout\Cart\SalesChannel\CartOrderRouteResponse;
  16. use Shopware\Core\Framework\Struct\ArrayStruct;
  17. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  18. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Wbfk\DigitalInvoice\Entity\Order\WbfkInvoiceDetailsOrderExtension;
  21. use Wbfk\DigitalInvoice\Entity\Order\WbfkInvoiceDetailsOrderExtensionEntity;
  22. use Wbfk\DigitalInvoice\Service\Utils\Utils;
  23. class CartOrderRouteDecorator extends AbstractCartOrderRoute implements EventSubscriberInterface
  24. {
  25.     public function __construct(
  26.         private readonly AbstractCartOrderRoute $decorated
  27.     ) {
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CartConvertedEvent::class => 'addDigitalInvoiceDataToOrder',
  33.             OrderConvertedEvent::class => 'addDigitalInvoiceDataToCart',
  34.         ];
  35.     }
  36.     public function getDecorated(): AbstractCartOrderRoute
  37.     {
  38.         return $this->decorated;
  39.     }
  40.     public function order(Cart $cartSalesChannelContext $contextRequestDataBag $data): CartOrderRouteResponse
  41.     {
  42.         $this->addAdditionalDataToCartSendFromConfirmationPage($cart$data);
  43.         return $this->decorated->order($cart$context$data);
  44.     }
  45.     private function addAdditionalDataToCartSendFromConfirmationPage(Cart $cartRequestDataBag $data): void
  46.     {
  47.         $additionalData = new ArrayStruct([
  48.             "invoiceType" => Utils::trimNull($data->get("wbfkDigitalInvoiceType")),
  49.             'bt10' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt10")),
  50.             'bt11' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt11")),
  51.             'bt12' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt12")),
  52.             'bt13' => Utils::trimNull($data->get("wbfkDigitalInvoiceBt13")),
  53.         ]);
  54.         $cart->addExtension(WbfkInvoiceDetailsOrderExtension::NAME$additionalData);
  55.     }
  56.     public function addDigitalInvoiceDataToOrder(CartConvertedEvent $event): void
  57.     {
  58.         $cart $event->getCart();
  59.         $convertedCart $event->getConvertedCart();
  60.         /** @var ?ArrayStruct $digitalInvoiceData */
  61.         $digitalInvoiceData $cart->getExtension(WbfkInvoiceDetailsOrderExtension::NAME);
  62.         if (!$digitalInvoiceData) {
  63.             return;
  64.         }
  65.         $convertedCart['extensions'] = $convertedCart['extensions'] ?? [];
  66.         $convertedCart['extensions'][WbfkInvoiceDetailsOrderExtension::NAME] = $digitalInvoiceData->all();
  67.         $event->setConvertedCart($convertedCart);
  68.     }
  69.     public function addDigitalInvoiceDataToCart(OrderConvertedEvent $event): void
  70.     {
  71.         /** @var WbfkInvoiceDetailsOrderExtensionEntity $digitalInvoiceData */
  72.         $digitalInvoiceData $event->getOrder()->getExtension(WbfkInvoiceDetailsOrderExtension::NAME);
  73.         if (!$digitalInvoiceData) {
  74.             return;
  75.         }
  76.         $convertedCart $event->getConvertedCart();
  77.         $convertedCart->addExtension(WbfkInvoiceDetailsOrderExtension::NAME, new ArrayStruct(array_merge(
  78.             $digitalInvoiceData->invoiceDataAsArray(),
  79.             ["id" => $digitalInvoiceData->getId()]
  80.         )));
  81.     }
  82. }