custom/plugins/OkeonlineKejeDeliveryAddress/src/Core/Checkout/Customer/Aggregate/CustomerAddress/Subscriber/CustomerAddressWrittenSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace Okeonline\KejeDeliveryAddress\Core\Checkout\Customer\Aggregate\CustomerAddress\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CustomerAddressWrittenSubscriber implements EventSubscriberInterface {
  12.     private $customerAddressRepository;
  13.     private $customerRepository;
  14.     public function __construct(
  15.         EntityRepository $customerAddressRepository,
  16.         EntityRepository $customerRepository
  17.     )
  18.     {
  19.         $this->customerAddressRepository $customerAddressRepository;
  20.         $this->customerRepository $customerRepository;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'setCustomerAddressSource',
  26.         ];
  27.     }
  28.     public function setCustomerAddressSource(EntityWrittenEvent $event): void
  29.     {
  30.         foreach($event->getWriteResults() as $entityWriteResult)
  31.         {
  32.             if($entityWriteResult->getOperation() === EntityWriteResult::OPERATION_INSERT)
  33.             {
  34.                 // newley created address (api/storefront/backoffice)
  35.                 // determine where the source is:
  36.                 switch (get_class($event->getContext()->getSource()))
  37.                 {
  38.                     case SalesChannelApiSource::class;
  39.                         $source 'storefront';
  40.                         break;
  41.                     default:
  42.                         $source 'default';
  43.                         break;
  44.                 }
  45.                 $customerAddressId $entityWriteResult->getPrimaryKey();
  46.                 /** @var CustomerAddressEntity $currentCustomerAddress */
  47.                 $criteria = new Criteria([$customerAddressId]);
  48.                 $currentCustomerAddress $this->customerAddressRepository->search($criteria$event->getContext())->first();
  49.                 // If there is not an extensuon with customerAddressSource, we should create it, and set correct source
  50.                 if (!$currentCustomerAddress->hasExtension('customerAddressSource')) {
  51.                     // add extension and set value
  52.                     $this->customerAddressRepository->update([
  53.                         [
  54.                             'id' => $customerAddressId,
  55.                             'customerAddressSource' => ['source' => $source]
  56.                         ]
  57.                     ], $event->getContext());
  58.                 }
  59. //                // Then, if it is storefront, set it als new default ShippingAddress.
  60. //                // This setting wil be undone when:
  61. //                    // Order is created
  62. //                    // User is logout/logged in
  63. //                if($source === 'storefront' && $currentCustomerAddress->getCustomerId())
  64. //                {
  65. //                    // set new addres as default shipping address
  66. //                    $this->customerRepository->update([
  67. //                        [
  68. //                            'id' => $currentCustomerAddress->getCustomerId(),
  69. //                            'defaultShippingAddressId' => $customerAddressId
  70. //                        ]
  71. //                    ], $event->getContext());
  72. //
  73. //                }
  74.             }
  75.         }
  76.     }
  77. }