| 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 |
if ( ! class_exists( 'RenderDataLayerGtm' ) ) : |
| 14 |
class RenderDataLayerGtm{ |
| 15 |
/** |
| 16 |
* Holds the values to be used in the fields callbacks |
| 17 |
*/ |
| 18 |
private $options; |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance of this class. |
| 22 |
* |
| 23 |
* @var object |
| 24 |
*/ |
| 25 |
protected static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Start up |
| 29 |
*/ |
| 30 |
private function __construct() |
| 31 |
{ |
| 32 |
add_action('wp_head', array($this, 'search_page_trigger'), 30); |
| 33 |
|
| 34 |
$options = get_option('tracking_option'); |
| 35 |
$checked = (isset($options['data_layer_google_tag_manager']) && $options['data_layer_google_tag_manager'] == 1) ? 1 : 0; |
| 36 |
if ($checked): |
| 37 |
if (!$this->hasValidRequirements()) { |
| 38 |
add_action('admin_notices', [$this, 'showAdminNotice']); |
| 39 |
return; |
| 40 |
} |
| 41 |
endif; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Return an instance of this class. |
| 46 |
* |
| 47 |
* @return object A single instance of this class. |
| 48 |
*/ |
| 49 |
public static function get_instance() { |
| 50 |
// If the single instance hasn't been set, set it now. |
| 51 |
if ( null === self::$instance ) { |
| 52 |
self::$instance = new self(); |
| 53 |
} |
| 54 |
|
| 55 |
return self::$instance; |
| 56 |
} |
| 57 |
|
| 58 |
public function search_page_trigger(){ |
| 59 |
|
| 60 |
$options = get_option('tracking_option'); |
| 61 |
$checked = ( isset($options['data_layer_google_tag_manager']) && $options['data_layer_google_tag_manager'] == 1) ? 1 : 0; |
| 62 |
if($checked): |
| 63 |
|
| 64 |
if(is_order_received_page()) { |
| 65 |
$this->mount_purchase_data_layer(); |
| 66 |
} |
| 67 |
endif; |
| 68 |
} |
| 69 |
|
| 70 |
private function mount_purchase_data_layer(){ |
| 71 |
$transactionId = empty($_GET[ 'order' ]) ? ($GLOBALS[ 'wp' ]->query_vars[ 'order-received' ] ? $GLOBALS[ 'wp' ]->query_vars[ 'order-received' ] : 0) : absint($_GET[ 'order' ]); |
| 72 |
|
| 73 |
if(!empty($transactionId)){ |
| 74 |
$order = wc_get_order( $transactionId ); |
| 75 |
$user = $order->get_user(); |
| 76 |
$products = $order->get_items(); |
| 77 |
$transactionTotal = $order->get_total(); |
| 78 |
$transactionTax = $order->get_total_tax(); |
| 79 |
$transactionShipping = $order->get_shipping_total(); |
| 80 |
|
| 81 |
$product_data = []; |
| 82 |
foreach ($order->get_items() as $item) { |
| 83 |
$sku = $item->get_product_id(); |
| 84 |
$price = $item->get_total(); |
| 85 |
|
| 86 |
$product_categories = get_the_terms($sku, 'product_cat'); |
| 87 |
if ((is_array($product_categories)) && (count($product_categories) > 0)) { |
| 88 |
$product_cat = array_pop($product_categories); |
| 89 |
$product_cat = $product_cat->name; |
| 90 |
} else { |
| 91 |
$product_cat = ''; |
| 92 |
} |
| 93 |
|
| 94 |
$product_data[] = [ |
| 95 |
'name' => $item['name'], //Required |
| 96 |
'sku' => $sku, //Required |
| 97 |
'category' => $product_cat, //Optional |
| 98 |
'price' => $price, //Required |
| 99 |
'quantity' => $item['qty'] //Required |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
$dataLayer = [ |
| 104 |
'transactionId'=>$transactionId, //Required |
| 105 |
// 'transactionAffiliation'=>, //Optional |
| 106 |
'transactionTotal'=> $transactionTotal, //Required |
| 107 |
'transactionShipping'=> $transactionShipping, //Optional |
| 108 |
'transactionTax'=> $transactionTax, //Optional |
| 109 |
'transactionProducts'=> $product_data |
| 110 |
]; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
$this->push_to_data_layer($dataLayer); |
| 115 |
} |
| 116 |
|
| 117 |
private function push_to_data_layer($dataLayer){ |
| 118 |
$encodedDataLayer = json_encode($dataLayer); |
| 119 |
$scriptTag = '<script data-cfasync="false" type="text/javascript">if(typeof dataLayer=="undefined"){dataLayer = []} dataLayer.push( %s );</script>'; |
| 120 |
echo sprintf($scriptTag, $encodedDataLayer); |
| 121 |
} |
| 122 |
|
| 123 |
private function hasValidRequirements() |
| 124 |
{ |
| 125 |
// return class_exists('WooCommerce') && floatval(WC()->version) >= 5.0; |
| 126 |
if ( ! function_exists( 'is_woocommerce_activated' ) ) { |
| 127 |
if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; } |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public function showAdminNotice() |
| 132 |
{ |
| 133 |
printf("<div class='notice notice-warning is-dismissible'> |
| 134 |
<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> |
| 135 |
<button type='button' class='notice-dismiss'> |
| 136 |
<span class='screen-reader-text'>Dismiss this notice.</span> |
| 137 |
</button> |
| 138 |
</div>"); |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
RenderDataLayerGtm::get_instance(); |
| 143 |
endif; |
| 144 |
|