<?php declare(strict_types=1);
namespace MadNoms\Subscriber;
use Shopware\Core\Checkout\Customer\CustomerEvents;
//use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Storefront\Controller\AddressController;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
class CustomerEventsSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $nomsCustomerAddressRepository;
private EntityRepositoryInterface $addressRepository;
public function __construct(
EntityRepositoryInterface $nomsCustomerAddressRepository,
EntityRepositoryInterface $addressRepository
)
{
$this->nomsCustomerAddressRepository = $nomsCustomerAddressRepository;
$this->addressRepository = $addressRepository;
}
public static function getSubscribedEvents()
{
return [
//CustomerRegisterEvent::class => 'onCreateCustomer',
AddressController::class => 'onSaveAddress',
CustomerEvents::MAPPING_ADDRESS_CREATE => 'onMappingAddress',
CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onMappingAddress',
CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onMappingAddress',
CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => 'onCustomerAddressLoaded'
];
}
public function onCustomerAddressLoaded(EntityLoadedEvent $event): void
{
$context = $event->getContext();
$addresses = $event->getEntities();
foreach($addresses as $address){
$id = $address->getId();
$criteria = (new Criteria())->addFilter(
new EqualsFilter('customerAddressId', $id),
);
$result = $this->nomsCustomerAddressRepository->search($criteria, $context);
$nomsCustomerAddress = $result->first();
$address->addExtension(
'nomsCustomerAddress',
$nomsCustomerAddress
);
if($address->getCustomFields()){
if(isset($address->getCustomFields()['nomsStreet']) and $address->getExtensions()['nomsCustomerAddress'] == null){
$this->nomsCustomerAddressRepository->create([
[
'customerAddressId' => $address->getId(),
'customerId' => $address->getCustomerId(),
'street' => $address->getCustomFields()['nomsStreet'],
'streetnumber' => $address->getCustomFields()['nomsStreetnumber']
]
], $context);
$criteria = (new Criteria())->addFilter(
new EqualsFilter('customerAddressId', $id),
);
$result = $this->nomsCustomerAddressRepository->search($criteria, $context);
$nomsCustomerAddress = $result->first();
$address->addExtension(
'nomsCustomerAddress',
$nomsCustomerAddress
);
}
}
}
}
/*public function onCreateCustomer(CustomerRegisterEvent $event): void
{
$context = $event->getSalesChannelContext()->getContext();
$street = $event->getCustomer()->getDefaultBillingAddress()->getCustomFields()['nomsStreet'];
$streetnumber = $event->getCustomer()->getDefaultBillingAddress()->getCustomFields()['nomsStreetnumber'];
$customerId = $event->getCustomer()->getId();
$customerBillingAddressId = $event->getCustomer()->getDefaultBillingAddress()->getId();
$customerShippingAddressId = $event->getCustomer()->getDefaultShippingAddress()->getId();
$this->nomsCustomerAddressRepository->create([
[
'customerAddressId' => $customerBillingAddressId,
'customerId' => $customerId,
'street' => $street,
'streetnumber' => $streetnumber
]
], $context);
if ($customerBillingAddressId != $customerShippingAddressId) {
$street = $event->getCustomer()->getDefaultShippingAddress()->getCustomFields()['nomsStreet'];
$streetnumber = $event->getCustomer()->getDefaultShippingAddress()->getCustomFields()['nomsStreetnumber'];
$this->nomsCustomerAddressRepository->create([
[
'customerAddressId' => $customerShippingAddressId,
'customerId' => $customerId,
'street' => $street,
'streetnumber' => $streetnumber
]
], $context);
}
}*/
public function onMappingAddress(DataMappingEvent $event): void
{
$data = $event->getInput();
$address = $event->getOutput();
//Set Street and Streetnumber in shopware orginal field
$address['customFields']['nomsStreet'] = $address['street'];
$address['customFields']['nomsStreetnumber'] = $data->get('streetnumber');
$address['street'] = $address['street'] .' '. $data->get('streetnumber');
if($data->get('id')){
$criteria = (new Criteria())->addFilter(
new EqualsFilter('customerAddressId', $data->get('id')),
);
$nomsCustomerAddressId = $this->nomsCustomerAddressRepository->searchIds($criteria, $event->getContext())->firstId();
if($nomsCustomerAddressId){
$this->nomsCustomerAddressRepository->update([
[
'id' => $nomsCustomerAddressId,
'street' => $address['customFields']['nomsStreet'],
'streetnumber' => $address['customFields']['nomsStreetnumber']
]
], $event->getContext());
}else{
$this->nomsCustomerAddressRepository->create([
[
'customerAddressId' => $data->get('id'),
'customerId' => $data->get('nomsId'),
'street' => $address['customFields']['nomsStreet'],
'streetnumber' => $address['customFields']['nomsStreetnumber']
]
], $event->getContext());
}
}
$event->setOutput($address);
}
}