<?php
namespace CioDefaultBillingAddress\Subscriber;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded'
];
}
public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $checkoutCartPageLoadedEvent)
{
$customer = $checkoutCartPageLoadedEvent->getSalesChannelContext()->getCustomer();
$activeBillingAddress = $customer->getActiveBillingAddress();
$billingAddress = new CustomerAddressEntity();
$billingAddress->setId(Uuid::randomHex());
$billingAddress->setCompany('EWE TEL GmbH');
$billingAddress->setFirstName(' ');
$billingAddress->setLastName(' ');
$billingAddress->setStreet('Cloppenburger Str. 310');
$billingAddress->setZipcode('26133');
$billingAddress->setCity('Oldenburg ');
if ($activeBillingAddress->getCountry()) { $billingAddress->setCountry($activeBillingAddress->getCountry()); }
$billingAddress->setCountryId($activeBillingAddress->getCountryId());
if ($activeBillingAddress->getCustomer()) { $billingAddress->setCustomer($activeBillingAddress->getCustomer()); }
$billingAddress->setCustomerId($activeBillingAddress->getCustomerId());
if ($activeBillingAddress->getSalutation()) { $billingAddress->setSalutation($activeBillingAddress->getSalutation()); }
if ($activeBillingAddress->getSalutationId()) { $billingAddress->setSalutationId($activeBillingAddress->getSalutationId()); }
if ($activeBillingAddress->getCountryState()) { $billingAddress->setCountryState($activeBillingAddress->getCountryState()); }
if ($activeBillingAddress->getCountryStateId()) { $billingAddress->setCountryStateId($activeBillingAddress->getCountryStateId()); }
$customer->setActiveBillingAddress($billingAddress);
}
}