@@ -49,8 +49,14 @@ public function prepare_email_for_preview( $email ) {
49
49
case 'WCS_Email_Customer_Notification_Auto_Renewal ' :
50
50
$ email ->set_object ( $ this ->get_dummy_subscription () );
51
51
break ;
52
+ case 'WCS_Email_Customer_Payment_Retry ' :
53
+ case 'WCS_Email_Payment_Retry ' :
54
+ $ email ->retry = $ this ->get_dummy_retry ( $ email ->object );
55
+ break ;
52
56
}
53
57
58
+ $ this ->add_placeholders ( $ email );
59
+
54
60
add_filter ( 'woocommerce_mail_content ' , [ $ this , 'clean_up_filters ' ] );
55
61
56
62
return $ email ;
@@ -139,13 +145,55 @@ private function get_dummy_address() {
139
145
return apply_filters ( 'woocommerce_subscriptions_email_preview_dummy_address ' , $ address , $ this ->email_type );
140
146
}
141
147
148
+ /**
149
+ * Creates a dummy retry for use when previewing failed subscription payment retry emails.
150
+ *
151
+ * @param WC_Order $order The order object to create a dummy retry for.
152
+ * @return WCS_Retry The dummy retry object.
153
+ */
154
+ private function get_dummy_retry ( $ order ) {
155
+
156
+ if ( ! class_exists ( 'WCS_Retry_Manager ' ) ) {
157
+ return null ;
158
+ }
159
+
160
+ $ order_id = is_a ( $ order , 'WC_Order ' ) ? $ order ->get_id () : 12345 ;
161
+ $ retry_rule = WCS_Retry_Manager::rules ()->get_rule ( 1 , $ order_id );
162
+
163
+ if ( is_a ( $ retry_rule , 'WCS_Retry_Rule ' ) ) {
164
+ $ interval = $ retry_rule ->get_retry_interval ();
165
+ $ raw_retry_rule = $ retry_rule ->get_raw_data ();
166
+ } else {
167
+ // If the retry rule is not found, use a default interval of 12 hours and an empty raw rule.
168
+ $ interval = 12 * HOUR_IN_SECONDS ;
169
+ $ raw_retry_rule = [];
170
+ }
171
+
172
+ return new WCS_Retry (
173
+ [
174
+ 'status ' => 'pending ' ,
175
+ 'order_id ' => $ order_id ,
176
+ 'date_gmt ' => gmdate ( 'Y-m-d H:i:s ' , time () + $ interval ),
177
+ 'rule_raw ' => $ raw_retry_rule ,
178
+ ]
179
+ );
180
+ }
181
+
142
182
/**
143
183
* Check if the email being previewed is a subscription email.
144
184
*
145
- * @return bool
185
+ * Subscription emails include:
186
+ * - WC_Subscriptions_Email::$email_classes - core subscription emails.
187
+ * - WC_Subscriptions_Email_Notifications::$email_classes - subscription notification emails (pre-renewal emails).
188
+ * - WCS_Email_Customer_Payment_Retry - customer payment retry emails.
189
+ * - WCS_Email_Payment_Retry - admin payment retry emails.
190
+ *
191
+ * @return bool Whether the email being previewed is a subscription email.
146
192
*/
147
193
private function is_subscription_email () {
148
- return isset ( WC_Subscriptions_Email::$ email_classes [ $ this ->email_type ] ) || isset ( WC_Subscriptions_Email_Notifications::$ email_classes [ $ this ->email_type ] );
194
+ return isset ( WC_Subscriptions_Email::$ email_classes [ $ this ->email_type ] )
195
+ || isset ( WC_Subscriptions_Email_Notifications::$ email_classes [ $ this ->email_type ] )
196
+ || in_array ( $ this ->email_type , [ 'WCS_Email_Customer_Payment_Retry ' , 'WCS_Email_Payment_Retry ' ], true );
149
197
}
150
198
151
199
/**
@@ -204,4 +252,52 @@ public function allow_early_renewals_during_preview( $can_renew_early, $subscrip
204
252
205
253
return $ can_renew_early ;
206
254
}
255
+
256
+ /**
257
+ * Adds custom placeholders for subscription emails.
258
+ *
259
+ * @param WC_Email $email The email object.
260
+ */
261
+ private function add_placeholders ( $ email ) {
262
+ if ( ! isset ( $ email ->placeholders ) ) {
263
+ return ;
264
+ }
265
+
266
+ $ placeholders = [];
267
+
268
+ switch ( $ this ->email_type ) {
269
+ case 'WCS_Email_Customer_Notification_Subscription_Expiration ' :
270
+ case 'WCS_Email_Customer_Notification_Manual_Trial_Expiration ' :
271
+ case 'WCS_Email_Customer_Notification_Auto_Trial_Expiration ' :
272
+ case 'WCS_Email_Customer_Notification_Manual_Renewal ' :
273
+ case 'WCS_Email_Customer_Notification_Auto_Renewal ' :
274
+ // Pull the real values from the email object (Order or Subscription) if available.
275
+ if ( is_a ( $ email ->object , 'WC_Subscription ' ) ) {
276
+ $ time_until_renewal = $ email ->get_time_until_date ( $ email ->object , 'next_payment ' );
277
+ $ customer_first_name = $ email ->object ->get_billing_first_name ();
278
+ } else {
279
+ $ time_until_renewal = human_time_diff ( time (), time () + WEEK_IN_SECONDS );
280
+ $ customer_first_name = 'John ' ;
281
+ }
282
+
283
+ $ placeholders ['{time_until_renewal} ' ] = $ time_until_renewal ;
284
+ $ placeholders ['{customers_first_name} ' ] = $ customer_first_name ;
285
+ break ;
286
+ case 'WCS_Email_Customer_Payment_Retry ' :
287
+ case 'WCS_Email_Payment_Retry ' :
288
+ $ retry_time = is_a ( $ email ->retry , 'WCS_Retry ' )
289
+ ? $ email ->retry ->get_time ()
290
+ : time () + ( 12 * HOUR_IN_SECONDS );
291
+
292
+ $ placeholders ['{retry_time} ' ] = wcs_get_human_time_diff ( $ retry_time );
293
+ break ;
294
+ }
295
+
296
+ // Merge placeholders without overriding existing ones, and only adding those in the email.
297
+ $ email ->placeholders = wp_parse_args (
298
+ $ placeholders ,
299
+ $ email ->placeholders
300
+ );
301
+ }
207
302
}
303
+
0 commit comments