custom/plugins/MbPayerAddress/src/Subscriber/OrderPayerAddressSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace MbPayerAddress\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Doctrine\DBAL\Connection;
  5. use MbPayerAddress\Entity\OrderPayerAddressEntity;
  6. use MojeBambino\Extensions\Struct\Kafelki;
  7. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  8. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Checkout\Order\OrderEvents;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Core\Framework\Struct\ArrayEntity;
  19. use Shopware\Core\Framework\Struct\ArrayStruct;
  20. use Shopware\Core\Framework\Uuid\Uuid;
  21. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  22. use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
  23. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  24. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  25. class OrderPayerAddressSubscriber implements EventSubscriberInterface {
  26.     private EntityRepository $customerAddressRespository;
  27.     private EntityRepository $orderPayerAddressRespository;
  28.     private EntityRepository $customerRespository;
  29.     
  30.     public function __construct(
  31.         EntityRepository $customerAddressRespository,
  32.         EntityRepository $orderPayerAddressRespository,
  33.         EntityRepository $customerRespository,
  34.     ){
  35.         $this->customerAddressRespository $customerAddressRespository;
  36.         $this->orderPayerAddressRespository $orderPayerAddressRespository;
  37.         $this->customerRespository $customerRespository;
  38.     }
  39.     
  40.     public static function getSubscribedEvents(): array{
  41.         return [
  42.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlacedEvent',
  43.             OrderEvents::ORDER_LOADED_EVENT => 'onOrderLoaded',
  44.         ];
  45.     }
  46.     
  47.     public function onCheckoutOrderPlacedEvent(CheckoutOrderPlacedEvent $event) {
  48.         $orderId $event->getOrder()->getId();
  49.         $customer $event->getOrder()->getOrderCustomer()->getCustomer();
  50.         $customFields $customer->getCustomFields();
  51.         $billingId $customer->getDefaultBillingAddressId();
  52.         $shippingId $customer->getDefaultShippingAddressId();
  53.         $addressId $customFields['payerAddressId'] ?? $billingId;
  54.         $type 'other';
  55.         
  56.         $id Uuid::randomHex();
  57.         $customFields $this->getShippingAndBilling($event->getOrder());
  58.         if($addressId == $billingId) {
  59.             $type 'billing';
  60.             $id $customFields['currentBillingOrderAddressId'];
  61.         } else if($addressId == $shippingId) {
  62.             $type 'shipping';
  63.             $id $customFields['currentShippingOrderAddressId'];
  64.         } 
  65.         $payerAddressEntity $this->customerAddressRespository
  66.             ->search(new Criteria([$addressId]), $event->getContext())
  67.             ->first();
  68.         $this->createOrderPayerAddress($orderId$payerAddressEntity$type$id$event->getContext());
  69.     }
  70.     
  71.     private function getShippingAndBilling($order) {
  72.         $customFieldsArr['currentBillingOrderAddressId'] = $order->getBillingAddressId();
  73.         if(!$order->getDeliveries()) return [];
  74.         $activeDelivery $order->getDeliveries()->getAt(0);
  75.         /** @var OrderDeliveryEntity $delivery */
  76.         foreach($order->getDeliveries() as $delivery) {
  77.             if($delivery->getShippingCosts()->getUnitPrice() > $activeDelivery->getShippingCosts()->getUnitPrice()) {
  78.                 $activeDelivery $delivery;
  79.             }
  80.         }
  81.         $customFieldsArr['currentShippingOrderAddressId']= $delivery->getShippingOrderAddressId();
  82.         return $customFieldsArr;
  83.     }
  84.     
  85.     private function createOrderPayerAddress(string $orderId$payerAddressEntitystring $typestring $id$context
  86.     {
  87.         $this->orderPayerAddressRespository->create([
  88.             [
  89.                 'id' => $id,
  90.                 'orderId' => $orderId,
  91.                 'company' => $payerAddressEntity->getCompany(),
  92.                 'vatId' => $payerAddressEntity->getCustomFields()['address_NIP'] ?? '',
  93.                 'firstName' => $payerAddressEntity->getFirstName(),
  94.                 'lastName' => $payerAddressEntity->getLastName(),
  95.                 'street' => $payerAddressEntity->getStreet(),
  96.                 'zipcode' => $payerAddressEntity->getZipcode(),
  97.                 'city' => $payerAddressEntity->getCity(),
  98.                 'phoneNumber' => $payerAddressEntity->getPhoneNumber(),
  99.                 'additionalAddressLine1' => $payerAddressEntity->getAdditionalAddressLine1(),
  100.                 'additionalAddressLine2' => $payerAddressEntity->getAdditionalAddressLine2(),
  101.                 
  102.                 'equalTo' => $type,
  103.                 'customFields' => $payerAddressEntity->getCustomFields()
  104.             ]
  105.         ], $context);
  106.     }
  107.     
  108.     public function onOrderLoaded(EntityLoadedEvent $event
  109.     {
  110.         $orderIds = [];
  111.         foreach ($event->getEntities() as $orderEntity) {
  112.             $orderIds[] = $orderEntity->getId();
  113.         }
  114.         $criteria = new Criteria();
  115.         $criteria->addFilter(new EqualsAnyFilter('orderId'$orderIds));
  116.         $payerAddressSearchResult $this->orderPayerAddressRespository
  117.             ->search($criteria$event->getContext());
  118.         
  119.         /** @var OrderEntity $orderEntity */
  120.         foreach ($event->getEntities() as $orderEntity) {
  121.             /** @var OrderPayerAddressEntity $payerAddress */
  122.             $payerAddress $payerAddressSearchResult->filter(function (OrderPayerAddressEntity $orderPayerAddressEntity) use ($orderEntity) {
  123.                 return $orderPayerAddressEntity->getOrderId() == $orderEntity->getId();
  124.             })->first();
  125.             
  126.             $orderEntity->addExtension('order_payer_address'$payerAddress);
  127.             $customFields $orderEntity->getCustomFields();
  128.             $shippingAndBilling $this->getShippingAndBilling($orderEntity);
  129.             
  130.             if($customFields !== null) {
  131.                 $customFields array_merge($customFields$shippingAndBilling);
  132.             } else {
  133.                 $customFields $shippingAndBilling;
  134.             }
  135.             if ($payerAddress != null) {
  136.                 $customFields['currentPayerOrderAddressId'] = $payerAddress->getId();
  137.             }
  138.             $orderEntity->setCustomFields($customFields);
  139.         }        
  140.     }
  141. }