custom/plugins/DIScoGA4/src/Resources/views/storefront/ga4/ga4-products-extensions.html.twig line 1

Open in your IDE?
  1. {% block disco_meta_extra_information_extensions_products %}
  2.     {% set productsExtension = context.context.extensions.disGA4ProductsExtension %}
  3.     {% if productsExtension is not null %}
  4.         {% block disco_meta_extra_information_extensions_products_initialisation %}
  5.             <script {% if app.session.get('csp_nonce') %}nonce="{{ app.session.get('csp_nonce') }}"{% endif %}
  6.                     type="text/javascript">
  7.                 window.ga4Product = {};
  8.             </script>
  9.         {% endblock %}
  10.         {% set bigCategoriesList = ga4GetCategoriesSQLCached(productsExtension,context.context) %}
  11.         {% block disco_meta_extra_information_extensions_products_loop %}
  12.             {% for product in productsExtension %}
  13.                 {% set price = product.calculatedPrice %}
  14.                 {% if product.calculatedPrices|length == 1 %}
  15.                     {% set price = product.calculatedPrices.first %}
  16.                 {% endif %}
  17.                 {% set listPrice = price.listPrice %}
  18.                 {% if listPrice.percentage > 0 %}
  19.                     {% set listingPrice = price.listPrice.price %}
  20.                 {% else %}
  21.                     {% set listingPrice = price.unitPrice %}
  22.                 {% endif %}
  23.                 {% if product.calculatedPrices.count > 0 %}
  24.                     {% set startPrice = product.calculatedPrices.first.unitPrice %}
  25.                     {% set hasGraduatedPrice = true %}
  26.                 {% else %}
  27.                     {% set startPrice = price.unitPrice %}
  28.                     {% set hasGraduatedPrice = false %}
  29.                 {% endif %}
  30.                 {% set variantText = '' %}
  31.                 {% if product.variation %}
  32.                     {% for variation in product.variation %}
  33.                         {% set variantText = variantText ~ ' ' ~ variation.group ~ ' ' ~ variation.option ~ ' -' %}
  34.                     {% endfor %}
  35.                 {% endif %}
  36.                 {% block disco_meta_extra_information_extensions_product_product_id %}
  37.                     {% set ga4Id = 'ga4-product-' ~ product.id %}
  38.                 {% endblock %}
  39.                 {% if product.manufacturer %}
  40.                     {% set itemBrand = product.manufacturer.translated.name %}
  41.                 {% else %}
  42.                     {% set itemBrand = ga4GetManufacturerSQL(product.manufacturerId,context.context) %}
  43.                 {% endif %}
  44.                 {% block disco_meta_extra_information_extensions_product_array_script %}
  45.                     <script {% if app.session.get('csp_nonce') %}nonce="{{ app.session.get('csp_nonce') }}"{% endif %}
  46.                             type="text/javascript">
  47.                         window.ga4Product['{{ product.id }}'] = {
  48.                             item_brand: '{{ itemBrand }}',
  49.                             item_name: '{{ product.translated.name }}',
  50.                             item_id: '{{ product.productNumber }}',
  51.                             currency: '{{ context.currency.translated.shortName }}',
  52.                             item_variant: '{{ variantText | trim(' ','left') | trim('-','right') }}',
  53.                             price: '{{ price.unitPrice }}',
  54.                             extra: {
  55.                                 minPurchase: '{{ product.minPurchase }}',
  56.                                 shopware_id: '{{ product.id }}',
  57.                                 realPrice: '{{ listingPrice }}',
  58.                                 item_startPrice: '{{ startPrice }}',
  59.                                 item_hasGraduatedPrice: '{{ hasGraduatedPrice }}',
  60.                                 taxRate: '{{ product.tax.taxRate }}',
  61.                                 cheapest: '{{ price.unitPrice }}',
  62.                             }
  63.                         };
  64.                         {% if controllerName|lower == 'product' %}
  65.                             window.ga4Product['{{ product.id }}']['item_list_id'] = 'product';
  66.                             window.ga4Product['{{ product.id }}']['item_list_name'] = 'product';
  67.                         {% elseif controllerName|lower == 'navigation' %}
  68.                             window.ga4Product['{{ product.id }}']['item_list_id'] = '{{ page.header.navigation.active.id }}';
  69.                             window.ga4Product['{{ product.id }}']['item_list_name'] = '{{ page.header.navigation.active.name }}';
  70.                         {% elseif controllerName|lower == 'search' %}
  71.                             window.ga4Product['{{ product.id }}']['item_list_id'] = 'search';
  72.                             window.ga4Product['{{ product.id }}']['item_list_name'] = 'search';
  73.                         {% endif %}
  74.                         {% if bigCategoriesList[product.id] %}
  75.                             {% set counter = 1 %}
  76.                             {% for key,value  in bigCategoriesList[product.id] %}
  77.                                 {% if counter == 1 %}
  78.                                     window.ga4Product['{{ product.id }}']['item_category'] = '{{ value }}';
  79.                                 {% else %}
  80.                                     window.ga4Product['{{ product.id }}']['item_category{{ counter }}'] = '{{ value }}';
  81.                                 {% endif %}
  82.                                 {% set counter = counter + 1 %}
  83.                             {% endfor %}
  84.                         {% endif %}
  85.                     </script>
  86.                 {% endblock %}
  87.             {% endfor %}
  88.         {% endblock %}
  89.     {% endif %}
  90. {% endblock %}