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

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\TH:i:s');
  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\TH:i:s');
  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($response); die;
  225.         curl_close($curl);
  226.         if($response) {
  227.             $data json_decode($response);
  228.             if(!empty($data->data->id)) {
  229.                 $this->logger->info('CDP Customer API: ', [
  230.                     'id' => $data->data->id,
  231.                     'responseCode' => $info['http_code'],
  232.                 ]);
  233.             }
  234.         }
  235.         return $response;
  236.     }
  237.     /**
  238.      * Get salutation by id.
  239.      *
  240.      * @param string $salutation
  241.      * @return string
  242.      */
  243.     public function getSalutation($salutation): string
  244.     {
  245.         if ($salutation == 'fcc8e0d6a4764bbba6e49791614f0ae1') {
  246.             $salutation 'Keine Angabe';
  247.         } elseif ($salutation == 'bc965dc8237d4a448a9e981663e48a45') {
  248.             $salutation 'Frau';
  249.         } elseif ($salutation == '13905f90029e4f638ac122ccb762aff4') {
  250.             $salutation 'Herr';
  251.         }
  252.         return $salutation;
  253.     }
  254.     /**
  255.      * Get country name by id.
  256.      *
  257.      * @param string $countryState
  258.      * @return string
  259.      */
  260.     public function getCountryState($countryState): string
  261.     {
  262.         switch($countryState) {
  263.             case '0ce33d413c904e92acc79c69f8a08d15':
  264.                 $countryState 'Hamburg';
  265.                 break;
  266.             case 'f3b08fd5d0a04ba59ba226c85bf59738':
  267.                 $countryState 'Bremen';
  268.                 break;
  269.             case 'd712d274bd574428a10d1e2d025e7ab6':
  270.                 $countryState 'Niedersachsen';
  271.                 break;
  272.             case 'd230986e91e8441c85e994c0be34089e':
  273.                 $countryState 'Mecklenburg-Vorpommern';
  274.                 break;
  275.             case 'd04dbef41d454c7eb6deb84a0c06a699':
  276.                 $countryState 'Brandenburg';
  277.                 break;
  278.             case 'b6c91701b703472c8a73e8df30cad101':
  279.                 $countryState 'Nordhein-Westfalen';
  280.                 break;
  281.             case '9fc46966f0234e9f8530186f4642bb3c':
  282.                 $countryState 'Schleswig-Holstein';
  283.                 break;
  284.             case '98def0aeb554407da2969936b41d34c2':
  285.                 $countryState 'Rheinland-Pfalz';
  286.                 break;
  287.             case '744f1bd0196a4156a8dc8f92605b6777':
  288.                 $countryState 'Sachsen-Anhalt';
  289.                 break;
  290.             case '6899f042a86c47c298789f12e44e6313':
  291.                 $countryState 'Sachsen';
  292.                 break;
  293.             case '670f4eda6c034eb49d729c71a2a4905d':
  294.                 $countryState 'Saarland';
  295.                 break;
  296.             case '574d6df925484487a1d0253dc9b1ab6f':
  297.                 $countryState 'Bayern';
  298.                 break;
  299.             case '24e210f44daf447db2173b7c9304b2ec':
  300.                 $countryState 'Baden-Württemberg ';
  301.                 break;
  302.             case '208691df26cc4788a2848e5f79fa2917':
  303.                 $countryState 'Berlin';
  304.                 break;
  305.             case '147f15c078424485b15a5a1671be7096':
  306.                 $countryState 'Thüringen';
  307.                 break;
  308.             case 'fce8394d0db04a8f9ecf7e6d82aa9e0c':
  309.                 $countryState 'Hessen';
  310.                 break;
  311.             default:
  312.                 $countryState '';
  313.         }
  314.         return $countryState;
  315.     }
  316. }