custom/plugins/EcBasicTheme/src/Storefront/Event/Subscriber/CXPDataSubscriber.php line 161

Open in your IDE?
  1. <?php
  2. namespace EcBasicTheme\Storefront\Event\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Shopware\Core\Framework\Event\DataMappingEvent;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Psr\Log\LoggerInterface;
  9. class CXPDataSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var LoggerInterface
  13.      */
  14.     private $logger;
  15.     /**
  16.      * StorefrontRenderEventSubscriber constructor.
  17.      * @param LoggerInterface $logger
  18.      */
  19.     public function __construct(LoggerInterface $logger)
  20.     {
  21.         $this->logger $logger;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CustomerRegisterEvent::class => 'onRegister',
  27.             CustomerEvents::CUSTOMER_ADDRESS_WRITTEN_EVENT => 'onCustomerAddressUpdate',
  28.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerUpdate'
  29.         ];
  30.     }
  31.     /**
  32.      * Send Customer data to CDP when he register
  33.      *
  34.      * @param CustomerRegisterEvent $event
  35.      * @return void
  36.      */
  37.     public function onRegister(CustomerRegisterEvent $event){
  38.         $customer $event-> getCustomer();
  39.         $customerNumber $customer->getId();
  40.         $salutation $this->getSalutation($customer->getSalutationId());
  41.         $firstName $customer->getFirstName();
  42.         $lastName $customer->getLastName();
  43.         $email =$customer->getEmail();
  44.         $title $customer->getTitle();
  45.         $address $customer->getDefaultBillingAddress();
  46.         $street $address->getStreet();
  47.         $zipcode =  $address->getZipcode();
  48.         $city $address->getCity();
  49.         $countryId $address->getCountryId();
  50.         $countryState $this->getCountryState($address->getCountryStateId());
  51.         $attributes = [
  52.             'salutation' => $salutation,
  53.             'title' => $title,
  54.             'firstname' => $firstName,
  55.             'lastname' => $lastName,
  56.             'email' => [
  57.                 'address' => $email,
  58.                 'permission' => 'Unknown',
  59.                 'trackingPermission' => 'Granted'
  60.             ],
  61.             'address' => [
  62.                 'line1' => $street,
  63.                 'city' => $city,
  64.                 'postalcode' => $zipcode,
  65.                 'province' => $countryState,
  66.                 'country' => 'germany',
  67.                 'line3' => 'DEMOSHOP'
  68.             ],
  69.             'identifiers' => [
  70.                 [
  71.                     'type' => 'email',
  72.                     'id' => $email
  73.                 ]
  74.             ]
  75.         ];
  76.         if (!empty($customer->getBirthday())) {
  77.             $attributes['birthdate'] = $customer->getBirthday()->format('Y-m-d');
  78.         }
  79.         $data = [
  80.             'data' => [
  81.                 'type' => 'demo-shop',
  82.                 'id' => $customerNumber,
  83.                 'attributes' => $attributes 
  84.             ]
  85.         ];
  86.         $response$this->executeCurl(json_encode($data));
  87.         return true;
  88.     }
  89.     /**
  90.      * Send data to CDP when customer update address
  91.      *
  92.      * @param EntityWrittenEvent $event
  93.      * @return void
  94.      */
  95.     public function onCustomerAddressUpdate(EntityWrittenEvent  $event)
  96.     {      
  97.         $input $event->getPayloads()[0];
  98.         //we doing this only on update not on create, this event is called everytime customer is saved.
  99.         //so it is called when we register customer too, this prevents it from being executed in that case
  100.         if(empty($input) || array_key_exists('createdAt'$input))
  101.         {
  102.             return true;
  103.         }
  104.         $customerNumber $input['customerId'];
  105.         $salutation $this->getSalutation($input['salutationId']);
  106.         $firstName $input['firstName'];
  107.         $lastName $input['lastName'];
  108.         $zipcode $input['zipcode'];
  109.         $street $input['street'];
  110.         $city $input['city'];
  111.         $countryState $this->getCountryState($input['countryStateId']);
  112.         $data = [
  113.             'data' => [
  114.                 'type' => 'demo-shop',
  115.                 'id' => $customerNumber,
  116.                 'attributes' => [
  117.                     'salutation' => $salutation,
  118.                     'firstname' => $firstName,
  119.                     'lastname' => $lastName,
  120.                     'address' => [
  121.                         'line1' => $street,
  122.                         'city' => $city,
  123.                         'postalcode' => $zipcode,
  124.                         'province' => $countryState,
  125.                         'country' => 'germany'
  126.                     ]
  127.                 ]
  128.             ]
  129.         ];
  130.         $response $this->executeCurl(json_encode($data));
  131.         return true;
  132.     }
  133.     /**
  134.      * Send data to CDP when customer update his email
  135.      *
  136.      * @param EntityWrittenEvent $event
  137.      * @return void
  138.      */
  139.     public function onCustomerUpdate(EntityWrittenEvent  $event)
  140.     { 
  141.         $input $event->getPayloads()[0];
  142.        
  143.         //we doing this only on update not on create, this event is called everytime customer is saved.
  144.         //so it is called when we register customer too, this prevents it from being executed in that case
  145.         if(empty($input) || array_key_exists('createdAt'$input))
  146.         {
  147.             return true;
  148.         }
  149.         $customerNumber $input['id'];
  150.         $data = [];
  151.         //if we have email, then we know it is email update change in shopware, otherwise it is update of personal info
  152.         if(array_key_exists("email",$input)) {
  153.             $email $input['email'];
  154.             $data = [
  155.                 'data' => [
  156.                     'type' => 'demo-shop',
  157.                     'id' => $customerNumber,
  158.                     'attributes' => [
  159.                         'email' => [
  160.                             'address' => $email,
  161.                             'permission' => 'Unknown',
  162.                             'trackingPermission' => 'Granted'
  163.                         ],
  164.                         'identifiers' => [
  165.                             [
  166.                                 'type' => 'email',
  167.                                 'id' => $email
  168.                             ]
  169.                         ]
  170.                     ]
  171.                 ]
  172.             ];
  173.         } elseif(array_key_exists("firstName",$input)) {
  174.             $salutation $this->getSalutation($input['salutationId']);
  175.             $firstName $input['firstName'];
  176.             $lastName $input['lastName'];
  177.             $birthday is_null($input['birthday']) ? '' :  $input['birthday']->format('Y-m-d');
  178.             
  179.             $data = [
  180.                 'data' => [
  181.                     'type' => 'demo-shop',
  182.                     'id' => $customerNumber,
  183.                     'attributes' => [
  184.                         'salutation' => $salutation,
  185.                         'firstname' => $firstName,
  186.                         'lastname' => $lastName,
  187.                         'birthdate' => $birthday
  188.                     ]
  189.                 ]
  190.             ];
  191.         }
  192.         if(!empty($data)) {
  193.             $response $this->executeCurl(json_encode($data));
  194.         }
  195.         return true;
  196.     }
  197.     /**
  198.      * Execute cURL request for CDP
  199.      *
  200.      * @param string $data
  201.      * @return string
  202.      */
  203.     public function executeCurl($data$method "POST"): string
  204.     {
  205.         $curl curl_init();
  206.     //    echo '<pre>'; print_r($data); die;
  207.         curl_setopt_array($curl, array(
  208.             CURLOPT_URL => 'https://cxp-api-dem.dymatrix.online/722a702302a7ed004cccb5b9f606c25c/cdp/api/customer?api-version=v1',
  209.             CURLOPT_RETURNTRANSFER => true,
  210.             CURLOPT_ENCODING => '',
  211.             CURLOPT_MAXREDIRS => 10,
  212.             CURLOPT_TIMEOUT => 0,
  213.             CURLOPT_FOLLOWLOCATION => true,
  214.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  215.             CURLOPT_CUSTOMREQUEST => $method,
  216.             CURLOPT_POSTFIELDS => $data,
  217.             CURLOPT_HTTPHEADER => array(
  218.                 'cxp-subscription-key: f7aa444af99f41e2890f7f223ee29262',
  219.                 'Content-Type: application/json'
  220.             ),
  221.         ));
  222.         $response curl_exec($curl);
  223.         $info curl_getinfo($curl);
  224.         // echo '<pre>'; print_r($data);
  225.         // echo '<pre>'; print_r($response); die;
  226.         curl_close($curl);
  227.         if($response) {
  228.             $data json_decode($response);
  229.             if(!empty($data->data->id)) {
  230.                 $this->logger->info('CDP Customer API: ', [
  231.                     'id' => $data->data->id,
  232.                     'responseCode' => $info['http_code'],
  233.                 ]);
  234.             }
  235.         }
  236.         return $response;
  237.     }
  238.     /**
  239.      * Get salutation by id.
  240.      *
  241.      * @param string $salutation
  242.      * @return string
  243.      */
  244.     public function getSalutation($salutation): string
  245.     {
  246.         if ($salutation == 'fcc8e0d6a4764bbba6e49791614f0ae1') {
  247.             $salutation 'Keine Angabe';
  248.         } elseif ($salutation == 'bc965dc8237d4a448a9e981663e48a45') {
  249.             $salutation 'Frau';
  250.         } elseif ($salutation == '13905f90029e4f638ac122ccb762aff4') {
  251.             $salutation 'Herr';
  252.         }
  253.         return $salutation;
  254.     }
  255.     /**
  256.      * Get country name by id.
  257.      *
  258.      * @param string $countryState
  259.      * @return string
  260.      */
  261.     public function getCountryState($countryState): string
  262.     {
  263.         switch($countryState) {
  264.             case '0ce33d413c904e92acc79c69f8a08d15':
  265.                 $countryState 'Hamburg';
  266.                 break;
  267.             case 'f3b08fd5d0a04ba59ba226c85bf59738':
  268.                 $countryState 'Bremen';
  269.                 break;
  270.             case 'd712d274bd574428a10d1e2d025e7ab6':
  271.                 $countryState 'Niedersachsen';
  272.                 break;
  273.             case 'd230986e91e8441c85e994c0be34089e':
  274.                 $countryState 'Mecklenburg-Vorpommern';
  275.                 break;
  276.             case 'd04dbef41d454c7eb6deb84a0c06a699':
  277.                 $countryState 'Brandenburg';
  278.                 break;
  279.             case 'b6c91701b703472c8a73e8df30cad101':
  280.                 $countryState 'Nordhein-Westfalen';
  281.                 break;
  282.             case '9fc46966f0234e9f8530186f4642bb3c':
  283.                 $countryState 'Schleswig-Holstein';
  284.                 break;
  285.             case '98def0aeb554407da2969936b41d34c2':
  286.                 $countryState 'Rheinland-Pfalz';
  287.                 break;
  288.             case '744f1bd0196a4156a8dc8f92605b6777':
  289.                 $countryState 'Sachsen-Anhalt';
  290.                 break;
  291.             case '6899f042a86c47c298789f12e44e6313':
  292.                 $countryState 'Sachsen';
  293.                 break;
  294.             case '670f4eda6c034eb49d729c71a2a4905d':
  295.                 $countryState 'Saarland';
  296.                 break;
  297.             case '574d6df925484487a1d0253dc9b1ab6f':
  298.                 $countryState 'Bayern';
  299.                 break;
  300.             case '24e210f44daf447db2173b7c9304b2ec':
  301.                 $countryState 'Baden-Württemberg ';
  302.                 break;
  303.             case '208691df26cc4788a2848e5f79fa2917':
  304.                 $countryState 'Berlin';
  305.                 break;
  306.             case '147f15c078424485b15a5a1671be7096':
  307.                 $countryState 'Thüringen';
  308.                 break;
  309.             case 'fce8394d0db04a8f9ecf7e6d82aa9e0c':
  310.                 $countryState 'Hessen';
  311.                 break;
  312.             default:
  313.                 $countryState '';
  314.         }
  315.         return $countryState;
  316.     }
  317. }