Answers for "enhanced ecommerce data layer for woocommerce"

PHP
0

enhanced ecommerce data layer for woocommerce

<script>
      dataLayer.push({
          'ecommerce': {
            'currencyCode': '<?php echo $order->get_order_currency(); ?>',
            'purchase': {
              'actionField':{
                'id': '<?php echo $order->get_order_number(); ?>',
                'affiliation': 'WooCommerce',
                'revenue': <?php echo number_format($order->get_subtotal(), 2, ".", ""); ?>,
                'tax': <?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>,
                'shipping': <?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>,
                <?php if($order->get_used_coupons()): ?>
                    'coupon': '<?php echo implode("-", $order->get_used_coupons()); ?>'
                <?php endif; ?>
              },
              'products': [
                  <?php
                    foreach($order->get_items() as $key => $item):
                      $product = $order->get_product_from_item( $item );
                      $variant_name = ($item['variation_id']) ? wc_get_product($item['variation_id']) : '';
                  ?>
                      {
                        'name': '<?php echo $item['name']; ?>',
                        'id': '<?php echo $item['product_id']; ?>',
                        'price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>',
                        'brand': '',
                        'category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>',
                        'variant': '<?php echo ($variant_name) ? implode("-", $variant_name->get_variation_attributes()) : ''; ?>',
                        'quantity': <?php echo $item['qty']; ?>
                      },
                  <?php endforeach; ?>
                ]
            }
          }
      });
    </script>
Posted by: Guest on September-16-2021

Browse Popular Code Answers by Language