custom/plugins/econdWebAnalytics/src/Storefront/Page/Checkout/Finish/Subscriber/CheckoutFinishPageLoadedEventSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ecd\econdWebAnalytics\Storefront\Page\Checkout\Finish\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  5. use Shopware\Core\Framework\Struct\ArrayEntity;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use \Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  9. use Doctrine\DBAL\Driver\Connection;
  10. class CheckoutFinishPageLoadedEventSubscriber implements EventSubscriberInterface
  11. {
  12.     private $connection$systemConfigService;
  13.     public function __construct(Connection $connectionSystemConfigService $systemConfigService)
  14.     {
  15.         $this->connection $connection;
  16.         $this->systemConfigService $systemConfigService;
  17.     }
  18.     /**
  19.      * @inheritDoc
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent'
  25.         ];
  26.     }
  27.     /**
  28.      * after finish page is loaded we will add an extension with informations about the basket
  29.      *
  30.      * @param CheckoutFinishPageLoadedEvent $event
  31.      */
  32.     public function onCheckoutFinishPageLoadedEvent(CheckoutFinishPageLoadedEvent $event)
  33.     {
  34.         $page $event->getPage();
  35.         $customerAdressEntity $event->getSalesChannelContext()->getCustomer()->getDefaultBillingAddress();
  36.         $lineItems $page->getOrder()->getLineItems()->getElements();
  37.         $lineItems $this->cleanLineItems($lineItems); //Removing Items with type!=product
  38.         $options $this->getSelectedProperties($lineItems);
  39.         $data['groups'] = $this->getGroup($lineItems);
  40.         $data['location'] = $this->getLocation($customerAdressEntity);
  41.         $data['pids'] = $this->getPids($lineItems);
  42.         $data['customerId'] = $this->encryptCustopmerId($event->getSalesChannelContext()->getCustomer());
  43.         $data['options'] = $options;
  44.         $page->addExtension('ANA', new ArrayEntity($data));
  45.     }
  46.     public function getSelectedProperties($lineItems) {
  47.         $config $this->systemConfigService->getDomain('econdWebAnalytics');
  48.         $data = [];
  49.         // if config is empty we get an error so we need a condition
  50.         if(gettype($config) != 'array' && empty($config) !== false) {
  51.             return $data;
  52.         }
  53.         $propertyNames = [];
  54.         foreach ($lineItems as $key => &$lineItem) {
  55.             $options $lineItem->getPayload()['options'];
  56.             foreach ($config as $k => $item) {
  57.                 $propertyNames[$lineItem->getId()][$k] = $this->connection->fetchAll(
  58.                     'SELECT property_group_translation.name FROM property_group_translation WHERE property_group_id = UNHEX(:id)',
  59.                     ['id' => $item]
  60.                 );
  61.             }
  62.             foreach($propertyNames as $ec_configurations) {
  63.                 foreach($ec_configurations as $ec_conf_option => $propertyName) {
  64.                     foreach ($options as $option) {
  65.                         foreach($propertyName as $ec_property) {
  66.                             if($option['group'] === $ec_property['name']){
  67.                                 $data[$lineItem->getId()][$ec_conf_option] = $option['option'];
  68.                             }
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         return $data;
  75.     }
  76.     public function encryptCustopmerId($customer)
  77.     {
  78.         return md5($customer->getEmail());
  79.     }
  80.     /**
  81.      * get the pid from the productnumber
  82.      *
  83.      * @param $lineItems
  84.      * @return array
  85.      */
  86.     public function getPids($lineItems)
  87.     {
  88.         foreach($lineItems as $lineItem) {
  89.             /**
  90.              * @var OrderLineItemEntity $lineItem
  91.              */
  92.             $productNumber $lineItem->getPayload()['productNumber'];
  93.             if(strpos($productNumber'.') == 0) {
  94.                 $pids[$lineItem->getProductId()] = $productNumber;
  95.                 continue;
  96.             }
  97.             $pids[$lineItem->getProductId()] = explode('.'$productNumber)[0];
  98.         }
  99.         return $pids;
  100.     }
  101.     /**
  102.      * get location of customer
  103.      *
  104.      * @param CustomerAddressEntity $customAdressEntity
  105.      * @return string
  106.      */
  107.     public function getLocation($customAdressEntity)
  108.     {
  109.         return $location $customAdressEntity->getCountry()->getIso() . "/" $customAdressEntity->getZipcode() . "/" $customAdressEntity->getCity();
  110.     }
  111.     /**
  112.      * get group of listItems
  113.      *
  114.      * @param $lineItems
  115.      * @return array
  116.      */
  117.     public function getGroup($lineItems)
  118.     {
  119.         foreach ($lineItems as $lineItem){
  120.             $categories $lineItem->getPayload()['categoryIds'];
  121.             if($categories != null) {
  122.                 array_shift($categories);
  123.                 $lastKey array_key_last($categories);
  124.                 $group '';
  125.                 foreach ($categories as $k => $category) {
  126.                     $categoryName $this->connection->fetchColumn(
  127.                         'SELECT category_translation.name FROM category_translation WHERE category_id = UNHEX(:id)',
  128.                         ['id' => $category]
  129.                     );
  130.                     $group .= $categoryName;
  131.                     if ($k != $lastKey) {
  132.                         $group .= '/';
  133.                     }
  134.                 }
  135.             } else {
  136.                 $group null;
  137.             }
  138.             $groups[$lineItem->getProductId()] = $group;
  139.         }
  140.         return $groups;
  141.     }
  142.     public function cleanLineItems($lineItems)
  143.     {
  144.         foreach($lineItems as $k => $lineItem) {
  145.             if($lineItem->getType() != 'product') {
  146.                 unset($lineItems[$k]);
  147.             }
  148.         }
  149.         return $lineItems;
  150.     }
  151. }