custom/plugins/MbPayerAddress/src/Subscriber/AppendPayerDataSubscriber.php line 54

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 AppendPayerDataSubscriber implements EventSubscriberInterface {
  26.     private EntityRepository $customerAddressRespository;
  27.     private EntityRepository $customerRespository;
  28.     
  29.     public function __construct(
  30.         EntityRepository $customerAddressRespository,
  31.         EntityRepository $customerRespository,
  32.     ){
  33.         $this->customerAddressRespository $customerAddressRespository;
  34.         $this->customerRespository $customerRespository;
  35.     }
  36.     
  37.     public static function getSubscribedEvents(): array{
  38.         return [
  39.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
  40.             AddressListingPageLoadedEvent::class => 'onAddressListingPageLoadedEvent',
  41.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent',
  42.             AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoadedEvent',
  43.         ];
  44.     }
  45.     
  46.     function onCheckoutConfirmPageLoadedEvent(CheckoutConfirmPageLoadedEvent $event): void {
  47.         $payerAddressId $this->getPayerAddressId($event);
  48.         $this->appendPayerAddressToPage($event$payerAddressId);
  49.     }
  50.     
  51.     function onAddressListingPageLoadedEvent(AddressListingPageLoadedEvent $event) {
  52.         $payerAddressId $this->getPayerAddressId($event);
  53.         
  54.         $currentAddressId $event->getRequest()->get('id');
  55.         if($currentAddressId == $payerAddressId) {
  56.             $event->getPage()->addExtension('sameAsPayer', new ArrayStruct(['sameAsPayer' => true]));
  57.         } else {
  58.             $event->getPage()->addExtension('sameAsPayer', new ArrayStruct(['sameAsPayer' => false]));
  59.         }
  60.         
  61.         $this->appendPayerAddressToPage($event$payerAddressId);
  62.     }
  63.     
  64.     public function onCheckoutFinishPageLoadedEvent(CheckoutFinishPageLoadedEvent $event) {
  65.         $payerAddressId $this->getPayerAddressId($event);
  66.         $this->appendPayerAddressToPage($event$payerAddressId);
  67.     }
  68.     
  69.     public function onAccountOverviewPageLoadedEvent(AccountOverviewPageLoadedEvent $event) {
  70.         $payerAddressId $this->getPayerAddressId($event);
  71.         $this->appendPayerAddressToPage($event$payerAddressId);
  72.     }
  73.     
  74.     
  75.     
  76.     private function getPayerAddressId($event) {
  77.         $id $event->getSalesChannelContext()->getCustomer()->getId();
  78.         
  79.         $dbCustomer $this->customerRespository
  80.             ->search(new Criteria([$id]), $event->getContext())
  81.             ->first();
  82.         
  83.         $customFields $dbCustomer->getCustomFields();
  84.         
  85.         if(isset($customFields['payerAddressId'])) {
  86.             return $customFields['payerAddressId'];
  87.         } 
  88.         return $event->getSalesChannelContext()->getCustomer()->getDefaultBillingAddressId();
  89.     }
  90.     
  91.     private function appendPayerAddressToPage($event$payerAddressId) {
  92.         $payerAddressEntity $this->customerAddressRespository
  93.             ->search(new Criteria([$payerAddressId]), $event->getContext())
  94.             ->first();
  95.         $event->getPage()->addExtension('payerAddress'$payerAddressEntity);
  96.     }
  97. }