<?php
namespace MbPayerAddress\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\DBAL\Connection;
use MbPayerAddress\Entity\OrderPayerAddressEntity;
use MojeBambino\Extensions\Struct\Kafelki;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
class AppendPayerDataSubscriber implements EventSubscriberInterface {
private EntityRepository $customerAddressRespository;
private EntityRepository $customerRespository;
public function __construct(
EntityRepository $customerAddressRespository,
EntityRepository $customerRespository,
){
$this->customerAddressRespository = $customerAddressRespository;
$this->customerRespository = $customerRespository;
}
public static function getSubscribedEvents(): array{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
AddressListingPageLoadedEvent::class => 'onAddressListingPageLoadedEvent',
CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent',
AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoadedEvent',
];
}
function onCheckoutConfirmPageLoadedEvent(CheckoutConfirmPageLoadedEvent $event): void {
$payerAddressId = $this->getPayerAddressId($event);
$this->appendPayerAddressToPage($event, $payerAddressId);
}
function onAddressListingPageLoadedEvent(AddressListingPageLoadedEvent $event) {
$payerAddressId = $this->getPayerAddressId($event);
$currentAddressId = $event->getRequest()->get('id');
if($currentAddressId == $payerAddressId) {
$event->getPage()->addExtension('sameAsPayer', new ArrayStruct(['sameAsPayer' => true]));
} else {
$event->getPage()->addExtension('sameAsPayer', new ArrayStruct(['sameAsPayer' => false]));
}
$this->appendPayerAddressToPage($event, $payerAddressId);
}
public function onCheckoutFinishPageLoadedEvent(CheckoutFinishPageLoadedEvent $event) {
$payerAddressId = $this->getPayerAddressId($event);
$this->appendPayerAddressToPage($event, $payerAddressId);
}
public function onAccountOverviewPageLoadedEvent(AccountOverviewPageLoadedEvent $event) {
$payerAddressId = $this->getPayerAddressId($event);
$this->appendPayerAddressToPage($event, $payerAddressId);
}
private function getPayerAddressId($event) {
$id = $event->getSalesChannelContext()->getCustomer()->getId();
$dbCustomer = $this->customerRespository
->search(new Criteria([$id]), $event->getContext())
->first();
$customFields = $dbCustomer->getCustomFields();
if(isset($customFields['payerAddressId'])) {
return $customFields['payerAddressId'];
}
return $event->getSalesChannelContext()->getCustomer()->getDefaultBillingAddressId();
}
private function appendPayerAddressToPage($event, $payerAddressId) {
$payerAddressEntity = $this->customerAddressRespository
->search(new Criteria([$payerAddressId]), $event->getContext())
->first();
$event->getPage()->addExtension('payerAddress', $payerAddressEntity);
}
}