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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
<?php
namespace YayMail\Shortcodes;
use YayMail\Utils\Helpers; use YayMail\Utils\SingletonTrait; use YayMail\Utils\Logger; /** * @method: static OrderMetaShortcodes init() */ class OrderMetaShortcodes {
use SingletonTrait;
private $logger;
protected function __construct() { $this->logger = new Logger(); add_filter( 'yaymail_extra_shortcodes', [ $this, 'get_order_meta_shortcodes' ], 10, 2 ); add_filter( 'yaymail_extra_shortcodes', [ $this, 'get_order_tax_shortcodes' ], 10, 2 ); }
public function get_order_tax_shortcodes( $shortcodes, $data ) { $order = $data['render_data']['order'] ?? null;
if ( ! $order ) { return $shortcodes; }
$tax_items = $order->get_items( 'tax' );
foreach ( $tax_items as $item_id => $item_tax ) { $tax_rate_id = $item_tax->get_rate_id(); $new_shortcode = [ 'name' => "yaymail_order_taxes_{$tax_rate_id}", 'description' => $item_tax->get_label(), 'group' => 'order_taxes', 'callback' => [ $this, 'order_tax_callback' ], 'callback_args' => [ 'item_tax' => $item_tax, ], ]; $shortcodes[] = $new_shortcode; }
return $shortcodes; }
public function order_tax_callback( $data, $shortcode_attrs = [] ) { $item_tax = $data['item_tax'] ?? '';
if ( empty( $item_tax ) || ! is_object( $item_tax ) ) { return ''; }
$tax_amount_total = $item_tax->get_tax_total(); $tax_shipping_total = $item_tax->get_shipping_tax_total(); $totals_taxes = $tax_amount_total + $tax_shipping_total; return wc_price( $totals_taxes ); }
/** * Init order meta shortcodes * * @param array $shortcodes The shortcodes array. * @param array $data The data array. * * @return array The shortcodes array. */ public function get_order_meta_shortcodes( $shortcodes, $data ) { $order = $data['render_data']['order'] ?? null;
if ( ! $order && ! empty( $data['render_data']['order_id'] ) ) { $order = wc_get_order( $data['render_data']['order_id'] ); }
if ( empty( $order ) ) { return $shortcodes; }
$metadata = self::get_allowed_order_meta_data( $order );
foreach ( $metadata as $meta_item ) { $data = $meta_item->get_data();
$field = Helpers::to_snake_case( $data['key'] );
$description = Helpers::snake_case_to_capitalized_words( $field ) . ' (' . $field . ')';
$callback = 'order_meta_callback'; if ( $field === 'pickup_date' || $field === 'delivery_date' ) { $callback = 'date_meta_callback'; }
$new_shortcode = [ 'name' => "yaymail_order_meta:{$field}", 'description' => $description, 'group' => 'order_meta', 'callback' => [ $this, $callback ], 'callback_args' => [ 'meta_item' => $meta_item, 'field' => $field, ], 'attributes' => [ 'is_date' => false, ], ];
$shortcodes[] = $new_shortcode; }//end foreach
return $shortcodes; }
public function order_meta_callback( $data, $shortcode_attrs = [] ) { $field = $data['field'] ?? ''; $meta_item = $data['meta_item'] ?? '';
if ( empty( $meta_item ) || ! method_exists( $meta_item, 'get_data' ) ) { return ''; }
$meta_data = $meta_item->get_data(); $value = $meta_data['value'];
if ( ! empty( $shortcode_attrs['is_date'] ) ) { $date = is_numeric( $value ) ? \DateTime::createFromFormat( 'U', $value ) : \DateTime::createFromFormat( 'Ymd', $value );
if ( $date ) { return date_i18n( wc_date_format(), $date->getTimestamp() ); }
$this->logger->log( "Order meta shortcode: field {$field} with value {$value} is not a valid date" ); return nl2br( $value ); }
if ( is_array( $value ) || is_object( $value ) ) { if ( is_object( $value ) ) { $value = (array) $value; } $str = isset( $value['extra'] ) && ( is_string( $value['extra'] ) || is_numeric( $value['extra'] ) ) ? $value['extra'] : $this->yaymail_flatten_to_string( $value ); return str_replace( '|', '<br />', $str ); }
return $value; }
public function date_meta_callback( $data ) { $meta_item = $data['meta_item'] ?? '';
if ( empty( $meta_item ) || ! method_exists( $meta_item, 'get_data' ) ) { return ''; }
$meta_data = $meta_item->get_data(); $value = $meta_data['value'];
if ( ! is_string( $value ) ) { return $value; }
if ( strtotime( $value ) ) { return date_i18n( wc_date_format(), strtotime( $value ) ); }
return $value; }
protected function yaymail_flatten_to_string( $value ) { if ( is_array( $value ) || is_object( $value ) ) { $result = []; foreach ( (array) $value as $v ) { $result[] = $this->yaymail_flatten_to_string( $v ); } return implode( ', ', $result ); } return strval( $value ); }
/** * Get allowed order meta data * Allow 3rd party to filter the which order meta data to be used in the email * * @since 4.1.0 */ public static function get_allowed_order_meta_data( $order ) {
if ( is_string( $order ) || is_numeric( $order ) ) { $order = wc_get_order( $order ); }
if ( ! $order && ! ( $order instanceof \WC_Order ) ) { return []; }
$order_meta_data = $order->get_meta_data();
return $order_meta_data;
// Maybe we can use this in the future // $order_id = $order->get_id();
// // ACF integration // if ( function_exists( 'acf_is_field_key' ) ) { // $order_meta_data = array_filter( // $order_meta_data, // function( $meta_item ) use ( $order_id ) { // $data = $meta_item->get_data(); // $search_key = strpos( $data['key'], '_' ) === 0 ? $data['key'] : '_' . $data['key'];
// $acf_ref = get_post_meta( $order_id, $search_key, true );
// if ( ! $acf_ref && strpos( $acf_ref, 'field_' ) !== 0 ) { // return true; // }
// return ! acf_is_field_key( $acf_ref ); // } // ); // }
// // Checkout fields by ThemeHigh // if ( class_exists( 'THWCFD_Utils' ) ) {
// $thwcfd_data = [ // \THWCFD_Utils::get_fields( 'billing' ), // \THWCFD_Utils::get_fields( 'shipping' ), // \THWCFD_Utils::get_fields( 'additional' ), // ];
// $custom_fields_sets = [];
// foreach ( $thwcfd_data as $set ) { // $custom_fields = []; // foreach ( array_keys( $set ) as $field_name ) { // $custom_fields[] = '_' . $field_name; // $custom_fields[] = $field_name; // } // $custom_fields_sets[] = $custom_fields; // }
// foreach ( $custom_fields_sets as $custom_fields ) { // if ( empty( $custom_fields ) ) { // continue; // } // $order_meta_data = array_filter( // $order_meta_data, // function( $meta_item ) use ( $custom_fields ) { // $data = $meta_item->get_data(); // return ! in_array( $data['key'], $custom_fields ); // } // ); // } // }//end if
// // Checkout fields editor by WooCommerce // if ( class_exists( 'WC_Checkout_Field_Editor' ) && method_exists( 'WC_Checkout_Field_Editor', 'get_fields' ) ) { // $custom_fields_sets = [ // \WC_Checkout_Field_Editor::get_fields( 'billing' ), // \WC_Checkout_Field_Editor::get_fields( 'shipping' ), // \WC_Checkout_Field_Editor::get_fields( 'additional' ), // ];
// foreach ( $custom_fields_sets as $custom_fields ) { // if ( empty( $custom_fields ) ) { // continue; // } // $custom_fields = array_filter( // $custom_fields, // function( $field ) { // return ! empty( $field['custom'] ); // } // ); // $order_meta_data = array_filter( // $order_meta_data, // function( $meta_item ) use ( $custom_fields ) { // $data = $meta_item->get_data(); // return ! in_array( $data['key'], array_keys( $custom_fields ) ); // } // ); // } // }//end if
// // Checkout fields by Acoweb // if ( defined( 'AWCFE_ORDER_META_KEY' ) ) { // $awcf_data = $order->get_meta( AWCFE_ORDER_META_KEY, true ); // $custom_fields_sets = [];
// foreach ( $awcf_data as $set ) { // $custom_fields = []; // foreach ( $set as $field ) { // $custom_fields[] = '_' . $field['name']; // } // $custom_fields[] = AWCFE_ORDER_META_KEY; // $custom_fields_sets[] = $custom_fields; // }
// foreach ( $custom_fields_sets as $custom_fields ) { // if ( empty( $custom_fields ) ) { // continue; // } // $order_meta_data = array_filter( // $order_meta_data, // function( $meta_item ) use ( $custom_fields ) { // $data = $meta_item->get_data(); // return ! in_array( $data['key'], $custom_fields ); // } // ); // } // }//end if
// return $order_meta_data; } }
|