Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Config\Services;
- use App\Libraries\Utils\ModuleUtils;
- use App\Libraries\Utils\DevUtils;
- use App\Modules\Orders\Models\OrderDomain;
- use App\Modules\Brands\Utils\BrandUtils;
- use App\Libraries\Utils\ProductCategoryUtils;
- if (ModuleUtils::isEnable('gTag')) {
- /**
- * @param $order OrderDomain;
- */
- function generateEventPurchase($order)
- {
- $row = $order->getRow();
- if ($row->ShowedGtag) {
- return;
- }
- Services::database()->table('orders')->set('ShowedGtag', 1)->where('OrderID', $row->OrderID)->update();
- $brandNames = BrandUtils::getBrandNames();
- $categoryNames = ProductCategoryUtils::getAllPaths();
- $return = new stdClass();
- @$return->ecommerce->purchase->actionField->id = $order->getPK();
- @$return->ecommerce->purchase->actionField->affiliation = 'okoffice.bg';
- @$return->ecommerce->purchase->actionField->revenue = $order->Total;
- @$return->ecommerce->purchase->actionField->tax = ($order->Total - $order->TotalWithoutVat);
- @$return->ecommerce->purchase->actionField->shipping = $order->Delivery;
- @$return->ecommerce->purchase->products = array();
- $products = $order->getProducts();
- $loop = 1;
- foreach ($products as $product) {
- $ProductPriceData = unserialize($product['ProductPriceData']);
- $productData = new stdClass();
- $productData->name = $product['ProductName'] . ' , ' . $product['ProductID'];
- $productData->id = $product['ProductID'];
- $productData->brand = $brandNames[$ProductPriceData->ProductBrandCode];
- $productData->category = $categoryNames[$ProductPriceData->ProductCategoryCode];
- $productData->price = $product['ProductPrice'] * 1.2;
- $productData->quantity = $product['Quantity'];
- @$return->ecommerce->purchase->products[] = $productData;
- $items[] = "{
- 'id': '{$product['ProductID']}',
- 'name': '{$product['ProductName']}',
- 'brand': '" . $brandNames[$ProductPriceData->ProductBrandCode] . "',
- 'category': '" . $categoryNames[$ProductPriceData->ProductCategoryCode] . "',
- 'list_position': '{$loop}',
- 'quantity': {$product['Quantity']},
- 'price': '" . $product['ProductPrice'] * 1.2 . "'
- }";
- $loop++;
- }
- echo "
- <script>
- gtag('event', 'purchase', {
- 'transaction_id' : " . $order->getPK() . ",
- 'affiliation' : 'okoffice.bg',
- 'value' : $order->Total,
- 'currency' : 'BGN',
- 'tax' :" . ($order->Total - $order->TotalWithoutVat) . ",
- 'shipping' : {$order->Delivery},
- 'items' : [
- " . implode('', $items) . "
- ]
- });
- </script>";
- }
- /**
- * @param $order OrderDomain;
- */
- function renderGTag($order)
- {
- $products = $order->getProducts();
- // echo "<script async src='https://www.googletagmanager.com/gtag/js?id=AW-994676596'></script>";
- $dataLayer = array();
- foreach ($products as $product) {
- $dataLayer[] = "
- {
- 'sku': '" . $product['ProductID'] . "',
- 'name': '" . $product['ProductName'] . "',
- 'price': " . $product['ProductPrice'] * 1.2 . ",
- 'quantity': " . $product['Quantity'] . "
- }";
- }
- echo "<script>ga('require', 'ecommerce');</script>";
- echo "<script>gtag('event', 'conversion', {
- 'send_to': 'AW-994676596/DcsHCJ--jaUBEPSeptoD',
- 'value': '{$order->Total}',
- 'currency': 'BGN',
- 'transaction_id': '{$order->OrderID}'
- });</script>";
- generateEventPurchase($order);
- foreach ($products as $product) {
- echo "<script>
- ga('ecommerce:addItem', {
- 'id': '{$product['ProductID']}',
- 'name': '{$product['ProductName']}',
- 'sku': '',
- 'category': '',
- 'price': '{$product['ProductPrice']}',
- 'quantity': '{$product['Quantity']}'
- });
- </script>";
- }
- echo "<script>ga('ecommerce:send');</script>";
- }
- Services::hooks()->add_action('CartComplete', 'renderGTag', 10, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement