1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\SeoBundle\Helper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Simple helper class to do E-Commerce Tracking. |
7
|
|
|
* Create one of these objects and pass it along to the google_analytics_ecommerce_tracking Twig function. |
8
|
|
|
* It'll create the correct calls to the GA script for you. |
9
|
|
|
* |
10
|
|
|
* API: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce?hl=nl |
11
|
|
|
* |
12
|
|
|
* Class Order |
13
|
|
|
*/ |
14
|
|
|
class Order |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string REQUIRED! The unique identifier for this Order/Transaction |
18
|
|
|
*/ |
19
|
|
|
protected $transactionID; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param int $id number The ID |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function setTransactionID($id) |
27
|
|
|
{ |
28
|
|
|
$this->transactionID = $id; |
|
|
|
|
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getTransactionID() |
37
|
|
|
{ |
38
|
|
|
return $this->transactionID; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string the name of the store that handled the Order/Transaction |
43
|
|
|
*/ |
44
|
|
|
protected $storeName = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name The name of the store |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function setStoreName($name) |
52
|
|
|
{ |
53
|
|
|
$this->storeName = $name; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getStoreName() |
62
|
|
|
{ |
63
|
|
|
return $this->storeName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* REQUIREd! |
68
|
|
|
* |
69
|
|
|
* @return string The total. Calculated automatically and returned as string. |
|
|
|
|
70
|
|
|
*/ |
71
|
|
|
public function getTotal() |
72
|
|
|
{ |
73
|
|
|
return $this->accumulatePropertyOnOrderItems('getValue'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return int|string |
78
|
|
|
*/ |
79
|
|
|
public function getTaxesTotal() |
80
|
|
|
{ |
81
|
|
|
return $this->accumulatePropertyOnOrderItems('getTaxes'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected $shippingTotal = ''; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $total string|number |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setShippingTotal($total) |
92
|
|
|
{ |
93
|
|
|
$this->shippingTotal = (float) $total; |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getShippingTotal() |
102
|
|
|
{ |
103
|
|
|
return $this->shippingTotal; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var array(of OrderItem) An array of OrderItem objects |
108
|
|
|
*/ |
109
|
|
|
public $orderItems = array(); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var string city the order was shipped to |
113
|
|
|
*/ |
114
|
|
|
protected $city = ''; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $city |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function setCity($city) |
122
|
|
|
{ |
123
|
|
|
$this->city = $city; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getCity() |
132
|
|
|
{ |
133
|
|
|
return $this->city; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var string state or province the order was shipped to |
138
|
|
|
*/ |
139
|
|
|
protected $stateOrProvince = ''; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $stateOrProvince |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function setStateOrProvince($stateOrProvince) |
147
|
|
|
{ |
148
|
|
|
$this->stateOrProvince = $stateOrProvince; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function getStateOrProvince() |
157
|
|
|
{ |
158
|
|
|
return $this->stateOrProvince; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @var string country the order was shipped to |
163
|
|
|
*/ |
164
|
|
|
protected $country = ''; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $country |
168
|
|
|
* |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function setCountry($country) |
172
|
|
|
{ |
173
|
|
|
$this->country = $country; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function getCountry() |
182
|
|
|
{ |
183
|
|
|
return $this->country; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Loops over the OrderItems and accumulates the value of the given property. Can also be a getter. |
188
|
|
|
* |
189
|
|
|
* @param $property |
190
|
|
|
* |
191
|
|
|
* @return int|string |
192
|
|
|
*/ |
193
|
|
|
private function accumulatePropertyOnOrderItems($property) |
194
|
|
|
{ |
195
|
|
|
if (\count($this->orderItems) == 0) { |
196
|
|
|
return ''; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$total = 0; |
200
|
|
|
foreach ($this->orderItems as $orderItem) { |
201
|
|
|
$total += $orderItem->$property(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $total; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.