Advertisement
fabi0

Untitled

Jun 7th, 2023 (edited)
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.60 KB | None | 0 0
  1. <?php
  2.  
  3. use Config\Services;
  4. use App\Libraries\Utils\ModuleUtils;
  5. use App\Libraries\Utils\DevUtils;
  6. use App\Modules\Orders\Models\OrderDomain;
  7. use App\Modules\Brands\Utils\BrandUtils;
  8. use App\Libraries\Utils\ProductCategoryUtils;
  9.  
  10. if (ModuleUtils::isEnable('gTag')) {
  11.     /**
  12.      * @param $order OrderDomain;
  13.      */
  14.     function generateEventPurchase($order)
  15.     {
  16.  
  17.         $row = $order->getRow();
  18.         if ($row->ShowedGtag) {
  19.             return;
  20.         }
  21.  
  22.         Services::database()->table('orders')->set('ShowedGtag', 1)->where('OrderID', $row->OrderID)->update();
  23.  
  24.         $brandNames = BrandUtils::getBrandNames();
  25.         $categoryNames = ProductCategoryUtils::getAllPaths();
  26.  
  27.         $return = new stdClass();
  28.         @$return->ecommerce->purchase->actionField->id = $order->getPK();
  29.         @$return->ecommerce->purchase->actionField->affiliation = 'okoffice.bg';
  30.         @$return->ecommerce->purchase->actionField->revenue = $order->Total;
  31.         @$return->ecommerce->purchase->actionField->tax = ($order->Total - $order->TotalWithoutVat);
  32.         @$return->ecommerce->purchase->actionField->shipping = $order->Delivery;
  33.         @$return->ecommerce->purchase->products = array();
  34.         $products = $order->getProducts();
  35.         $loop = 1;
  36.         foreach ($products as $product) {
  37.             $ProductPriceData = unserialize($product['ProductPriceData']);
  38.             $productData = new stdClass();
  39.             $productData->name = $product['ProductName'] . ' , ' . $product['ProductID'];
  40.             $productData->id = $product['ProductID'];
  41.             $productData->brand = $brandNames[$ProductPriceData->ProductBrandCode];
  42.             $productData->category = $categoryNames[$ProductPriceData->ProductCategoryCode];
  43.             $productData->price = $product['ProductPrice'] * 1.2;
  44.             $productData->quantity = $product['Quantity'];
  45.             @$return->ecommerce->purchase->products[] = $productData;
  46.  
  47.             $items[] = "{
  48.      'id': '{$product['ProductID']}',
  49.      'name': '{$product['ProductName']}',
  50.      'brand': '" . $brandNames[$ProductPriceData->ProductBrandCode] . "',
  51.      'category': '" . $categoryNames[$ProductPriceData->ProductCategoryCode] . "',
  52.      'list_position': '{$loop}',
  53.      'quantity': {$product['Quantity']},
  54.      'price': '" . $product['ProductPrice'] * 1.2 . "'
  55.    }";
  56.  
  57.             $loop++;
  58.         }
  59.  
  60.         echo "
  61.        <script>
  62.            gtag('event', 'purchase', {
  63.                'transaction_id' :  " . $order->getPK() . ",
  64.                'affiliation' : 'okoffice.bg',
  65.                'value' : $order->Total,
  66.                'currency' : 'BGN',
  67.                'tax' :" . ($order->Total - $order->TotalWithoutVat) . ",
  68.                'shipping' : {$order->Delivery},
  69.                'items' : [
  70.                    " . implode('', $items) . "
  71.                ]
  72.            });
  73.        </script>";
  74.  
  75.     }
  76.  
  77.     /**
  78.      * @param $order OrderDomain;
  79.      */
  80.     function renderGTag($order)
  81.     {
  82.  
  83.  
  84.         $products = $order->getProducts();
  85.         //        echo "<script async src='https://www.googletagmanager.com/gtag/js?id=AW-994676596'></script>";
  86.  
  87.         $dataLayer = array();
  88.         foreach ($products as $product) {
  89.             $dataLayer[] = "
  90.                {
  91.                   'sku': '" . $product['ProductID'] . "',
  92.                   'name': '" . $product['ProductName'] . "',
  93.                   'price': " . $product['ProductPrice'] * 1.2 . ",  
  94.                   'quantity': " . $product['Quantity'] . "
  95.                }";
  96.         }
  97.  
  98.  
  99.         echo "<script>ga('require', 'ecommerce');</script>";
  100.         echo "<script>gtag('event', 'conversion', {
  101.                    'send_to': 'AW-994676596/DcsHCJ--jaUBEPSeptoD',
  102.                    'value': '{$order->Total}',
  103.                    'currency': 'BGN',
  104.                    'transaction_id': '{$order->OrderID}'
  105.                    });</script>";
  106.  
  107.  
  108.         generateEventPurchase($order);
  109.  
  110.         foreach ($products as $product) {
  111.             echo "<script>
  112.  
  113.                    ga('ecommerce:addItem', {
  114.                        'id': '{$product['ProductID']}',
  115.                        'name': '{$product['ProductName']}',
  116.                        'sku': '',
  117.                        'category': '',
  118.                        'price': '{$product['ProductPrice']}',
  119.                        'quantity': '{$product['Quantity']}'
  120.                    });
  121.                </script>";
  122.         }
  123.  
  124.         echo "<script>ga('ecommerce:send');</script>";
  125.  
  126.     }
  127.  
  128.     Services::hooks()->add_action('CartComplete', 'renderGTag', 10, 1);
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement