custom/plugins/KlikensteenThemeCustomChanges/src/Subscriber/AddressChangedSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. namespace Klikensteen\ThemeCustomChanges\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Framework\Event\DataMappingEvent;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Doctrine\DBAL\Connection;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. /**
  14.  * Class AllVariantDetailPage
  15.  *
  16.  * Used to collect the information of the variants
  17.  */
  18. class AddressChangedSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var ContainerInterface
  22.      */
  23.     private $container;
  24.     /**
  25.      * @var Connection
  26.      */
  27.     private $connection;
  28.     /**
  29.      * @var RequestStack
  30.      */
  31.     protected RequestStack $requestStack;
  32.     /**
  33.      * @var EntityRepository
  34.      */
  35.     private EntityRepository $customerRepo;
  36.     public function __construct(
  37.         ContainerInterface $container,
  38.         RequestStack $requestStack,
  39.         EntityRepository $customerRepo
  40.     ) {
  41.         $this->container $container;
  42.         $this->requestStack $requestStack;
  43.         $this->connection $this->container->get(Connection::class);
  44.         $this->customerRepo $customerRepo;
  45.     }
  46.     /**
  47.      * @return string[]
  48.      */
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onAddessSave',
  53.             // CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'onAddessSave',
  54.             //  ProductListingCriteriaEvent::class => 'onProductListingCriteria'
  55.         ];
  56.     }
  57.     public function onAddessSave(DataMappingEvent $event)
  58.     {
  59.         $request $this->requestStack->getMainRequest();
  60.         $customFields $request->request->get('custom_fields');
  61.         $vatIds $request->request->get('vatIds');
  62.         $customer $request->attributes->get('sw-sales-channel-context')->getCustomer();
  63.         $kvkNumber $customFields['kvk_number'];
  64.         $orderReference $customFields['order_reference'];
  65.         if ($customFields) {
  66.             try {
  67.                 $this->connection->executeStatement(
  68.                     'UPDATE customer
  69.                 SET custom_fields = JSON_SET(
  70.                     custom_fields,
  71.                     "$.kvk_number",
  72.                     ' $kvkNumber '
  73.                 ) WHERE id = :customer_id',
  74.                     ['customer_id' => Uuid::fromHexToBytes($customer->id)]
  75.                 );
  76.                 $this->connection->executeStatement(
  77.                     'UPDATE customer
  78.                 SET custom_fields = JSON_SET(
  79.                     custom_fields,
  80.                     "$.order_reference",
  81.                     ' $orderReference '
  82.                 ) WHERE id = :customer_id',
  83.                     ['customer_id' => Uuid::fromHexToBytes($customer->id)]
  84.                 );
  85.             } catch (\Exception $e) {
  86.                 dump($e);
  87.             }
  88.             if ($vatIds[0] != "") {
  89.                 $this->customerRepo->update([
  90.                     [
  91.                         'id' => $customer->id,
  92.                         'vatIds' => $vatIds
  93.                     ]
  94.                 ], Context::createDefaultContext());
  95.             }
  96.         }
  97.     }
  98.     public function onProductListingCriteria(ProductListingCriteriaEvent $event)
  99.     {
  100.         $criteria $event->getCriteria();
  101.         $event->getCriteria()->addAssociation('properties');
  102.         $event->getCriteria()->addAssociation('properties.group');
  103.     }
  104. }