-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-datalayer.php
236 lines (175 loc) · 5.91 KB
/
wc-datalayer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/*
* Plugin Name: Datalayer Events for WooCommerce (Raceworks)
* Description: Ecommerce data layer events for WooCommerce
* Version: 1.0.0
* Author: gnar software
* Author URI: https://www.gnar.co.uk/
* License: GPLv2 or later
* Text Domain: wc-datalayer-gs
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'GSDL_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'GSDL_JS_DIR', plugin_dir_url( __FILE__ ) . '/js' );
class GSDL_datalayer_wc {
public function __construct() {
// Register scripts
add_action( 'wp_enqueue_scripts', [$this, 'enqueueScripts'] );
}
/**
* Enqueue Scripts
*/
public function enqueueScripts() {
// bail if WC isn't active
if (!class_exists('WooCommerce')) {
return;
}
// enqueue script
wp_enqueue_script( 'gsdl_wc_datalayer', GSDL_JS_DIR . '/gsdl_wc_datalayer.js', array( 'jquery' ), '1.0.8' );
// setup vars
$GSDL_Vars = [];
// product view & add to cart data
if (is_product()) {
$GSDL_Vars = $this->getProductData();
}
// init checkout
if (is_checkout()) {
$GSDL_Vars = $this->getCartData();
}
// purchase data
if (is_order_received_page()) {
$GSDL_Vars = $this->getOrderData();
}
// add vars to script
wp_add_inline_script( 'gsdl_wc_datalayer', 'const GSDL_Vars = ' . json_encode($GSDL_Vars), 'before' );
}
/**
* Get cart data for init checkout
*
* @return array $GSDL_Vars
*/
public function getCartData() {
$cart = WC()->cart;
$GSDL_Vars['products'] = [];
foreach ( WC()->cart->get_cart() as $key => $item ) {
$productID = $item['product_id'];
if (!empty($item['variation_id'])) {
$productID = $item['variation_id'];
}
$productObj = wc_get_product($productID);
$cartItem = [
'name' => $item['data']->get_title(),
'id' => $productObj->get_sku(),
'quantity' => $item['quantity'],
'price' => $item['data']->get_price()
];
array_push($GSDL_Vars['products'], $cartItem);
}
return $GSDL_Vars;
}
/**
* Get product data for item view
*
* @return array $GSDL_Vars
*/
public function getProductData() {
global $wp_query;
$post_id = $wp_query->post->ID;
if (empty($post_id)) {
return [];
}
$productObj = wc_get_product($post_id);
$GSDL_Vars = [];
// variable
if (!empty($productObj) && $productObj->is_type('variable')) {
// parent
$GSDL_Vars['parent'] = [
'name' => $productObj->get_name(),
'id' => $productObj->get_sku(),
'price' => $productObj->get_price(),
'category' => $this->getProductCat($productObj->get_id()),
'currency' => get_woocommerce_currency()
];
// variations
$GSDL_Vars['variations'] = [];
$variations = $productObj->get_children();
foreach ($variations as $variationID) {
$variationObj = wc_get_product($variationID);
$variation = [
'name' => $variationObj->get_name(),
'id' => $variationObj->get_sku(),
'price' => $variationObj->get_price(),
'category' => $this->getProductCat($variationID),
'currency' => get_woocommerce_currency()
];
$GSDL_Vars['variations'][$variationID] = $variation;
}
}
// simple
else {
$GSDL_Vars = [
'name' => $productObj->get_name(),
'id' => $productObj->get_sku(),
'price' => $productObj->get_price(),
'category' => $this->getProductCat($productObj->get_id()),
'currency' => get_woocommerce_currency()
];
}
return $GSDL_Vars;
}
/**
* Get order data for purchase
*
* @return array $GSDL_Vars
*/
public function getOrderData() {
global $wp;
$orderID = $wp->query_vars['order-received'];
$order = wc_get_order($orderID);
$GSDL_Vars = [
'id' => $order->get_id(),
'revenue' => $order->get_total(),
'tax' => $order->get_total_tax(),
'shipping' => $order->get_shipping_total(),
'products' => [],
'currency' => $order->get_currency()
];
foreach ($order->get_items() as $item) {
$productID = '';
$productObj = $item->get_product();
if ($productObj->is_type('variable')) {
$productID = $item->get_variation_id();
}
else {
$productID = $item->get_product_id();
}
$productData = (object) [
'id' => $productID,
'name' => $item->get_name(),
'price' => $item->get_total(),
'category' => $this->getProductCat($productID),
'quantity' => $item->get_quantity()
];
array_push($GSDL_Vars['products'], $productData);
}
return $GSDL_Vars;
}
/**
* Get first product category
*
* @param int $productID
* @return string $productFirstCat
*/
public function getProductCat($productID) {
$productFirstCat = '';
$terms = get_the_terms($productID, 'product_cat');
if (!empty($terms)) {
$productFirstCat = $terms[0]->name;
}
return $productFirstCat;
}
}
new GSDL_datalayer_wc();
?>