custom/plugins/WbmTagManagerAnalytics/src/Utility/SessionUtility.php line 18

Open in your IDE?
  1. <?php
  2. namespace Wbm\TagManagerAnalytics\Utility;
  3. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  4. class SessionUtility
  5. {
  6.     public const ATTRIBUTE_NAME 'wbm-stored-datalayer';
  7.     public const UPDATE_FLAG 'wbm-stored-shouldUpdate';
  8.     public const ADDCART_UPDATEFLAG_VALUE 'cartaddprice';
  9.     public const ADDCART_CART_ITEMS 'wbm-stored-addCart-addedCartItems';
  10.     public static function injectSessionVars(array $dataLayersSessionInterface $session): array
  11.     {
  12.         if (!$session->has(self::UPDATE_FLAG)) {
  13.             return $dataLayers;
  14.         }
  15.         if ($session->get(self::UPDATE_FLAG) === self::ADDCART_UPDATEFLAG_VALUE) {
  16.             try {
  17.                 $lineItems $session->get(self::ADDCART_CART_ITEMS);
  18.                 foreach ($dataLayers as $key => $dataLayer) {
  19.                     $dataLayer json_decode($dataLayertrue512JSON_THROW_ON_ERROR);
  20.                     if (empty($dataLayer)) {
  21.                         continue;
  22.                     }
  23.                     $conversion 0;
  24.                     foreach ($dataLayer['ecommerce']['items'] as $itemKey => $product) {
  25.                         $lineItemValue $lineItems[$product['item_id']];
  26.                         $product['price'] = $lineItemValue;
  27.                         $conversion += $lineItemValue $product['quantity'];
  28.                         $dataLayer['ecommerce']['items'][$itemKey] = $product;
  29.                     }
  30.                     $dataLayer['ecommerce']['value'] = $conversion;
  31.                     $dataLayer json_encode($dataLayerJSON_THROW_ON_ERROR);
  32.                     $dataLayers[$key] = $dataLayer;
  33.                 }
  34.             } catch (\Throwable $t) {
  35.                 // just to make sure session vars are removed on error
  36.             }
  37.             $session->remove(self::UPDATE_FLAG);
  38.             $session->remove(self::ADDCART_CART_ITEMS);
  39.         }
  40.         return $dataLayers;
  41.     }
  42. }