custom/plugins/b2bsellerscore/src/Components/Order/Subscriber/OrderSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\Order\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\B2bPlatformContext;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. class OrderSubscriber implements EventSubscriberInterface
  13. {
  14.     private RequestStack $requestStack;
  15.     private EntityRepository $customerAddressRepository;
  16.     public function __construct(
  17.         RequestStack     $requestStack,
  18.         EntityRepository $customerAddressRepository
  19.     )
  20.     {
  21.         $this->requestStack $requestStack;
  22.         $this->customerAddressRepository $customerAddressRepository;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             CartConvertedEvent::class => 'onCartConvertedEvent'
  28.         ];
  29.     }
  30.     public function onCartConvertedEvent(CartConvertedEvent $event)
  31.     {
  32.         $convertedCart $event->getConvertedCart();
  33.         $convertedCart['b2bOrderExtension'] = [];
  34.         $convertedCart $this->addAdditionalOrderFields($convertedCart);
  35.         $convertedCart $this->addLineItemCustomFields($convertedCart);
  36.         $convertedCart $this->setOrderAdresses($convertedCart$event->getContext());
  37.         $convertedCart $this->setPaymentMethod($convertedCart);
  38.         $customFields $event->getSalesChannelContext()->getCustomer()->getCustomFields();
  39.         if (isset($customFields['b2b_payment_condition_number'])) {
  40.             $convertedCart['customFields']['b2b_order_payment_condition'] = $customFields['b2b_payment_condition_number'];
  41.         }
  42.         $event->setConvertedCart($convertedCart);
  43.         if (!$event->getSalesChannelContext()->hasExtension('b2bPlatformContext')) {
  44.             return;
  45.         }
  46.         /** @var B2bPlatformContext $b2bPlatformContext */
  47.         $b2bPlatformContext $event->getSalesChannelContext()->getExtension('b2bPlatformContext');
  48.         if ($b2bPlatformContext->isSalesRepresentative()) {
  49.             $convertedCart['customFields']['b2b_order_sales_representative_id'] = $b2bPlatformContext->getSalesRepresentative()->getId();
  50.             $convertedCart['b2bOrderExtension']['salesRepresentativeId'] = $b2bPlatformContext->getSalesRepresentative()->getId();
  51.         }
  52.         if ($b2bPlatformContext->isEmployee()) {
  53.             $convertedCart['customFields']['b2b_order_customer_employee_id'] = $b2bPlatformContext->getEmployee()->getId();
  54.             $convertedCart['b2bOrderExtension']['employeeId'] = $b2bPlatformContext->getEmployee()->getId();
  55.         }
  56.         $event->setConvertedCart($convertedCart);
  57.     }
  58.     private function addAdditionalOrderFields($convertedCart)
  59.     {
  60.         $request $this->requestStack->getCurrentRequest();
  61.         if (empty($request)) {
  62.             return $convertedCart;
  63.         }
  64.         $convertedCart['customFields']['b2b_order_customer_note'] = $request->get('b2bCustomerNote''');
  65.         $convertedCart['customFields']['b2b_order_customer_commission'] = $request->get('b2bCustomerCommission''');
  66.         $convertedCart['customFields']['b2b_order_customer_employee_id'] = $request->get('b2bCustomerEmployeeId'null);
  67.         $convertedCart['customFields']['b2b_order_sales_representative_id'] = $request->get('b2bOrderSalesRepresentativeId'null);
  68.         $convertedCart['b2bOrderExtension']['employeeId'] = $request->get('b2bCustomerEmployeeId'null);
  69.         $convertedCart['b2bOrderExtension']['salesRepresentativeId'] = $request->get('b2bOrderSalesRepresentativeId'null);
  70.         return $convertedCart;
  71.     }
  72.     private function addLineItemCustomFields($convertedCart)
  73.     {
  74.         foreach ($convertedCart['lineItems'] as &$lineItem) {
  75.             $customFields $lineItem['customFields'] ?? [];
  76.             $comment $lineItem['payload']['comment'] ?? null;
  77.             $customFields['b2b_order_line_item_comment'] = $comment;
  78.             $lineItem['customFields'] = $customFields;
  79.         }
  80.         return $convertedCart;
  81.     }
  82.     private function setOrderAdresses($convertedCartContext $context)
  83.     {
  84.         $request $this->requestStack->getCurrentRequest();
  85.         if (empty($request)) {
  86.             return $convertedCart;
  87.         }
  88.         // works only for one delivery
  89.         if (!empty($request->get('shippingMethodId'))) {
  90.             $convertedCart['deliveries'][0]['shippingMethodId'] = $request->get('shippingMethodId');
  91.         }
  92.         if (!empty($request->get('billingAddressId'))) {
  93.             $billingAddress $this->getAddressbyAddressId($request->get('billingAddressId'), $context);
  94.             $newBillingAddressId Uuid::randomHex();
  95.             $convertedCart['billingAddressId'] = $newBillingAddressId;
  96.             $convertedCart['addresses'] = [[
  97.                 "id" => $newBillingAddressId,
  98.                 "company" => $billingAddress->getCompany(),
  99.                 "salutationId" => $billingAddress->getSalutationId(),
  100.                 "firstName" => $billingAddress->getFirstName(),
  101.                 "lastName" => $billingAddress->getLastName(),
  102.                 "street" => $billingAddress->getStreet(),
  103.                 "zipcode" => $billingAddress->getZipcode(),
  104.                 "city" => $billingAddress->getCity(),
  105.                 "countryId" => $billingAddress->getCountryId()
  106.             ]];
  107.         }
  108.         if (!empty($request->get('shippingAddressId'))) {
  109.             $shippingAddress $this->getAddressbyAddressId($request->get('shippingAddressId'), $context);
  110.             $convertedCart['deliveries'][0]['shippingOrderAddress'] = [
  111.                 "id" => Uuid::randomHex(),
  112.                 "company" => $shippingAddress->getCompany(),
  113.                 "salutationId" => $shippingAddress->getSalutationId(),
  114.                 "firstName" => $shippingAddress->getFirstName(),
  115.                 "lastName" => $shippingAddress->getLastName(),
  116.                 "street" => $shippingAddress->getStreet(),
  117.                 "zipcode" => $shippingAddress->getZipcode(),
  118.                 "city" => $shippingAddress->getCity(),
  119.                 "countryId" => $shippingAddress->getCountryId()
  120.             ];
  121.         }
  122.         return $convertedCart;
  123.     }
  124.     private function setPaymentMethod(array $convertedCart)
  125.     {
  126.         $request $this->requestStack->getCurrentRequest();
  127.         if (empty($request)) {
  128.             return $convertedCart;
  129.         }
  130.         // works only for one delivery
  131.         if (!empty($request->get('paymentMethodId'))) {
  132.             $convertedCart['transactions'][0]['paymentMethodId'] = $request->get('paymentMethodId');
  133.         }
  134.         return $convertedCart;
  135.     }
  136.     private function getAddressbyAddressId(string $addressIdContext $context): ?CustomerAddressEntity
  137.     {
  138.         $criteria = new Criteria([$addressId]);
  139.         return $this->customerAddressRepository->search($criteria$context)->first();
  140.     }
  141. }