2
2
3
3
namespace Texty \Notifications ;
4
4
5
- class OrderAdmin extends Notification {
5
+ class OrderBase extends Notification {
6
6
7
7
/**
8
8
* @var object
9
9
*/
10
- private $ order ;
11
-
12
- /**
13
- * Initialize
14
- */
15
- public function __construct () {
16
- $ this ->title = __ ( 'New Order (admin) ' , 'texty ' );
17
- $ this ->id = 'order_admin ' ;
18
- $ this ->group = 'wc ' ;
19
- $ this ->default_recipients = ['administrator ' ];
20
-
21
- $ this ->default = <<<'EOD'
22
- New order received #{order_id}, paid via {payment_method}.
23
-
24
- Customer: {customer_name} ({customer_email})
25
- Status: {status}
26
- Order Total: {order_total}
27
- EOD;
28
- }
10
+ protected $ order ;
29
11
30
12
/**
31
13
* Set the user ID
@@ -55,17 +37,15 @@ public function get_message() {
55
37
foreach ( $ this ->replacement_keys () as $ search => $ method ) {
56
38
$ value = method_exists ( $ this ->order , $ method ) ? $ this ->order ->$ method () : '' ;
57
39
58
- if ( 'customer_name ' === $ search ) {
59
- $ value = $ this ->order ->get_user ()->display_name ;
60
- }
61
-
62
40
if ( 'order_total ' === $ search ) {
63
41
$ value = strip_tags ( html_entity_decode ( $ value ) );
64
42
}
65
43
66
44
$ message = str_replace ( '{ ' . $ search . '} ' , $ value , $ message );
67
45
}
68
46
47
+ $ message = $ this ->replace_global_keys ( $ message );
48
+
69
49
return $ message ;
70
50
}
71
51
@@ -91,11 +71,48 @@ public function replacement_keys() {
91
71
'payment_method ' => 'get_payment_method_title ' ,
92
72
'shipping_method ' => 'get_shipping_method ' ,
93
73
'transaction_id ' => 'get_transaction_id ' ,
94
- 'customer_name ' => 'customer_name ' ,
95
- 'customer_email ' => 'get_billing_email ' ,
74
+ 'billing_name ' => 'get_formatted_billing_full_name ' ,
75
+ 'billing_email ' => 'get_billing_email ' ,
96
76
'order_total ' => 'get_formatted_order_total ' ,
97
77
'shipping_total ' => 'get_shipping_total ' ,
98
78
'tax_total ' => 'get_total_tax ' ,
99
79
];
100
80
}
81
+
82
+ public function send () {
83
+ if ( ! $ this ->enabled () ) {
84
+ return ;
85
+ }
86
+
87
+ $ meta_key = '_texty_ ' . $ this ->get_id ();
88
+ $ has_sent = $ this ->order ->get_meta ( $ meta_key , true );
89
+
90
+ // if we've already sent the message, don't send again
91
+ if ( $ has_sent ) {
92
+ return ;
93
+ }
94
+
95
+ // mark as sent
96
+ $ this ->order ->add_meta_data ( $ meta_key , 1 );
97
+ $ this ->order ->save_meta_data ();
98
+
99
+ if ( 'user ' === $ this ->get_type () ) {
100
+ $ number = $ this ->order ->get_billing_phone ();
101
+
102
+ $ recipients = $ number ? [ $ number ] : [];
103
+ } else {
104
+ $ recipients = $ this ->get_recipients ();
105
+ }
106
+
107
+ if ( ! $ recipients ) {
108
+ return ;
109
+ }
110
+
111
+ $ content = $ this ->get_message ();
112
+ $ gateway = texty ()->gateways ();
113
+
114
+ foreach ( $ recipients as $ number ) {
115
+ $ gateway ->send ( $ number , $ content );
116
+ }
117
+ }
101
118
}
0 commit comments