<?php declare(strict_types=1);
namespace Ecd\econdWebAnalytics\Storefront\Page\Checkout\Finish\Subscriber;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use \Symfony\Component\EventDispatcher\EventSubscriberInterface;
use \Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Doctrine\DBAL\Driver\Connection;
class CheckoutFinishPageLoadedEventSubscriber implements EventSubscriberInterface
{
private $connection, $systemConfigService;
public function __construct(Connection $connection, SystemConfigService $systemConfigService)
{
$this->connection = $connection;
$this->systemConfigService = $systemConfigService;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent'
];
}
/**
* after finish page is loaded we will add an extension with informations about the basket
*
* @param CheckoutFinishPageLoadedEvent $event
*/
public function onCheckoutFinishPageLoadedEvent(CheckoutFinishPageLoadedEvent $event)
{
$page = $event->getPage();
$customerAdressEntity = $event->getSalesChannelContext()->getCustomer()->getDefaultBillingAddress();
$lineItems = $page->getOrder()->getLineItems()->getElements();
$lineItems = $this->cleanLineItems($lineItems); //Removing Items with type!=product
$options = $this->getSelectedProperties($lineItems);
$data['groups'] = $this->getGroup($lineItems);
$data['location'] = $this->getLocation($customerAdressEntity);
$data['pids'] = $this->getPids($lineItems);
$data['customerId'] = $this->encryptCustopmerId($event->getSalesChannelContext()->getCustomer());
$data['options'] = $options;
$page->addExtension('ANA', new ArrayEntity($data));
}
public function getSelectedProperties($lineItems) {
$config = $this->systemConfigService->getDomain('econdWebAnalytics');
$data = [];
// if config is empty we get an error so we need a condition
if(gettype($config) != 'array' && empty($config) !== false) {
return $data;
}
$propertyNames = [];
foreach ($lineItems as $key => &$lineItem) {
$options = $lineItem->getPayload()['options'];
foreach ($config as $k => $item) {
$propertyNames[$lineItem->getId()][$k] = $this->connection->fetchAll(
'SELECT property_group_translation.name FROM property_group_translation WHERE property_group_id = UNHEX(:id)',
['id' => $item]
);
}
foreach($propertyNames as $ec_configurations) {
foreach($ec_configurations as $ec_conf_option => $propertyName) {
foreach ($options as $option) {
foreach($propertyName as $ec_property) {
if($option['group'] === $ec_property['name']){
$data[$lineItem->getId()][$ec_conf_option] = $option['option'];
}
}
}
}
}
}
return $data;
}
public function encryptCustopmerId($customer)
{
return md5($customer->getEmail());
}
/**
* get the pid from the productnumber
*
* @param $lineItems
* @return array
*/
public function getPids($lineItems)
{
foreach($lineItems as $lineItem) {
/**
* @var OrderLineItemEntity $lineItem
*/
$productNumber = $lineItem->getPayload()['productNumber'];
if(strpos($productNumber, '.') == 0) {
$pids[$lineItem->getProductId()] = $productNumber;
continue;
}
$pids[$lineItem->getProductId()] = explode('.', $productNumber)[0];
}
return $pids;
}
/**
* get location of customer
*
* @param CustomerAddressEntity $customAdressEntity
* @return string
*/
public function getLocation($customAdressEntity)
{
return $location = $customAdressEntity->getCountry()->getIso() . "/" . $customAdressEntity->getZipcode() . "/" . $customAdressEntity->getCity();
}
/**
* get group of listItems
*
* @param $lineItems
* @return array
*/
public function getGroup($lineItems)
{
foreach ($lineItems as $lineItem){
$categories = $lineItem->getPayload()['categoryIds'];
if($categories != null) {
array_shift($categories);
$lastKey = array_key_last($categories);
$group = '';
foreach ($categories as $k => $category) {
$categoryName = $this->connection->fetchColumn(
'SELECT category_translation.name FROM category_translation WHERE category_id = UNHEX(:id)',
['id' => $category]
);
$group .= $categoryName;
if ($k != $lastKey) {
$group .= '/';
}
}
} else {
$group = null;
}
$groups[$lineItem->getProductId()] = $group;
}
return $groups;
}
public function cleanLineItems($lineItems)
{
foreach($lineItems as $k => $lineItem) {
if($lineItem->getType() != 'product') {
unset($lineItems[$k]);
}
}
return $lineItems;
}
}