| 1 |
<?php |
| 2 |
/** |
| 3 |
* Wp Tracking Register Codes class |
| 4 |
* |
| 5 |
* @package WooCommerc/Classes/API |
| 6 |
* @version 2.11.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class RenderDataLayerGtm extends Wp_Tracking_Codes{ |
| 14 |
/** |
| 15 |
* Holds the values to be used in the fields callbacks |
| 16 |
*/ |
| 17 |
private $options; |
| 18 |
|
| 19 |
/** |
| 20 |
* Start up |
| 21 |
*/ |
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
add_action('wp_head', array($this, 'search_page_trigger'), 30); |
| 25 |
|
| 26 |
$options = get_option('tracking_option'); |
| 27 |
$checked = (isset($options['data_layer_google_tag_manager']) && $options['data_layer_google_tag_manager'] == 1) ? 1 : 0; |
| 28 |
if ($checked): |
| 29 |
if (!$this->hasValidRequirements()) { |
| 30 |
add_action('admin_notices', [$this, 'showAdminNotice']); |
| 31 |
return; |
| 32 |
} |
| 33 |
endif; |
| 34 |
} |
| 35 |
|
| 36 |
public function search_page_trigger(){ |
| 37 |
|
| 38 |
$options = get_option('tracking_option'); |
| 39 |
$checked = ( isset($options['data_layer_google_tag_manager']) && $options['data_layer_google_tag_manager'] == 1) ? 1 : 0; |
| 40 |
if($checked): |
| 41 |
|
| 42 |
if(is_order_received_page()) { |
| 43 |
$this->mount_purchase_data_layer(); |
| 44 |
} |
| 45 |
endif; |
| 46 |
} |
| 47 |
|
| 48 |
private function mount_purchase_data_layer(){ |
| 49 |
$transactionId = empty($_GET[ 'order' ]) ? ($GLOBALS[ 'wp' ]->query_vars[ 'order-received' ] ? $GLOBALS[ 'wp' ]->query_vars[ 'order-received' ] : 0) : absint($_GET[ 'order' ]); |
| 50 |
|
| 51 |
if(!empty($transactionId)){ |
| 52 |
$order = wc_get_order( $transactionId ); |
| 53 |
$user = $order->get_user(); |
| 54 |
$products = $order->get_items(); |
| 55 |
$transactionTotal = $order->get_total(); |
| 56 |
$transactionTax = $order->get_total_tax(); |
| 57 |
$transactionShipping = $order->get_shipping_total(); |
| 58 |
|
| 59 |
$product_data = []; |
| 60 |
foreach ($order->get_items() as $item) { |
| 61 |
$sku = $item->get_product_id(); |
| 62 |
$price = $item->get_total(); |
| 63 |
|
| 64 |
$product_categories = get_the_terms($sku, 'product_cat'); |
| 65 |
if ((is_array($product_categories)) && (count($product_categories) > 0)) { |
| 66 |
$product_cat = array_pop($product_categories); |
| 67 |
$product_cat = $product_cat->name; |
| 68 |
} else { |
| 69 |
$product_cat = ''; |
| 70 |
} |
| 71 |
|
| 72 |
$product_data[] = [ |
| 73 |
'name' => $item['name'], //Required |
| 74 |
'sku' => $sku, //Required |
| 75 |
'category' => $product_cat, //Optional |
| 76 |
'price' => $price, //Required |
| 77 |
'quantity' => $item['qty'] //Required |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
$dataLayer = [ |
| 82 |
'transactionId'=>$transactionId, //Required |
| 83 |
// 'transactionAffiliation'=>, //Optional |
| 84 |
'transactionTotal'=> $transactionTotal, //Required |
| 85 |
'transactionShipping'=> $transactionShipping, //Optional |
| 86 |
'transactionTax'=> $transactionTax, //Optional |
| 87 |
'transactionProducts'=> $product_data |
| 88 |
]; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
$this->push_to_data_layer($dataLayer); |
| 93 |
} |
| 94 |
|
| 95 |
private function push_to_data_layer($dataLayer){ |
| 96 |
$encodedDataLayer = json_encode($dataLayer); |
| 97 |
$scriptTag = '<script data-cfasync="false" type="text/javascript">dataLayer.push( %s );</script>'; |
| 98 |
echo sprintf($scriptTag, $encodedDataLayer); |
| 99 |
} |
| 100 |
|
| 101 |
private function hasValidRequirements() |
| 102 |
{ |
| 103 |
// return class_exists('WooCommerce') && floatval(WC()->version) >= 5.0; |
| 104 |
if ( ! function_exists( 'is_woocommerce_activated' ) ) { |
| 105 |
if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; } |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public function showAdminNotice() |
| 110 |
{ |
| 111 |
echo "<div class='notice notice-warning is-dismissible'> |
| 112 |
<p><b>DataLayer Google Tag Manager for Woocommerce</b> requires Woocommerce activated. <a href='/wp-admin/options-general.php?page=wp-tracking-codes&desativate_datalayer=true'>Disable function</a>.</p> |
| 113 |
<button type='button' class='notice-dismiss'> |
| 114 |
<span class='screen-reader-text'>Dismiss this notice.</span> |
| 115 |
</button> |
| 116 |
</div>"; |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
$RenderDataLayerGtm = new RenderDataLayerGtm(); |
| 123 |
|