@@ -13,6 +13,71 @@ class WC_Subscriptions_Email_Notifications {
13
13
public static function init () {
14
14
15
15
add_action ( 'woocommerce_email_classes ' , __CLASS__ . '::add_emails ' , 10 , 1 );
16
+
17
+ add_action ( 'woocommerce_init ' , __CLASS__ . '::hook_notification_emails ' );
18
+
19
+ add_filter ( 'woocommerce_order_actions ' , array ( __CLASS__ , 'add_notification_actions ' ), 10 , 1 );
20
+
21
+ add_action (
22
+ 'woocommerce_order_action_wcs_customer_notification_free_trial_expiration ' ,
23
+ function ( $ order ) {
24
+ /**
25
+ * Send Trial expiration notification to the customer.
26
+ *
27
+ * @since 8.0.0
28
+ *
29
+ * @param int $subscription_id
30
+ */
31
+ do_action ( 'woocommerce_scheduled_subscription_customer_notification_trial_expiration ' , $ order ->get_id () );
32
+ },
33
+ 10 ,
34
+ 1
35
+ );
36
+ add_action (
37
+ 'woocommerce_order_action_wcs_customer_notification_subscription_expiration ' ,
38
+ function ( $ order ) {
39
+ /**
40
+ * Send Subscription expiration notification to the customer.
41
+ *
42
+ * @since 8.0.0
43
+ *
44
+ * @param int $subscription_id
45
+ */
46
+ do_action ( 'woocommerce_scheduled_subscription_customer_notification_expiration ' , $ order ->get_id () );
47
+ },
48
+ 10 ,
49
+ 1
50
+ );
51
+ add_action (
52
+ 'woocommerce_order_action_wcs_customer_notification_manual_renewal ' ,
53
+ function ( $ order ) {
54
+ /**
55
+ * Send Manual renewal notification to the customer.
56
+ *
57
+ * @since 8.0.0
58
+ *
59
+ * @param int $subscription_id
60
+ */
61
+ do_action ( 'woocommerce_scheduled_subscription_customer_notification_manual_renewal ' , $ order ->get_id () );
62
+ },
63
+ 10 ,
64
+ 1
65
+ );
66
+ add_action (
67
+ 'woocommerce_order_action_wcs_customer_notification_auto_renewal ' ,
68
+ function ( $ order ) {
69
+ /**
70
+ * Send Automatic Renewal notification to the customer.
71
+ *
72
+ * @since 8.0.0
73
+ *
74
+ * @param int $subscription_id
75
+ */
76
+ do_action ( 'woocommerce_scheduled_subscription_customer_notification_auto_renewal ' , $ order ->get_id () );
77
+ },
78
+ 10 ,
79
+ 1
80
+ );
16
81
}
17
82
18
83
/**
@@ -29,23 +94,97 @@ public static function add_emails( $email_classes ) {
29
94
return $ email_classes ;
30
95
}
31
96
97
+ public static function hook_notification_emails () {
98
+ add_action ( 'woocommerce_scheduled_subscription_customer_notification_auto_renewal ' , array ( __CLASS__ , 'send_notification ' ) );
99
+ add_action ( 'woocommerce_scheduled_subscription_customer_notification_manual_renewal ' , array ( __CLASS__ , 'send_notification ' ) );
100
+ add_action ( 'woocommerce_scheduled_subscription_customer_notification_trial_expiration ' , array ( __CLASS__ , 'send_notification ' ) );
101
+ add_action ( 'woocommerce_scheduled_subscription_customer_notification_expiration ' , array ( __CLASS__ , 'send_notification ' ) );
102
+ }
103
+
104
+ public static function send_notification ( $ subscription_id ) {
105
+
106
+ // Init email classes.
107
+ $ emails = WC ()->mailer ()->get_emails ();
108
+
109
+ if ( ! ( $ emails ['WCS_Email_Customer_Notification_Auto_Renewal ' ] instanceof WCS_Email_Customer_Notification_Auto_Renewal
110
+ && $ emails ['WCS_Email_Customer_Notification_Manual_Renewal ' ] instanceof WCS_Email_Customer_Notification_Manual_Renewal
111
+ && $ emails ['WCS_Email_Customer_Notification_Subscription_Expiration ' ] instanceof WCS_Email_Customer_Notification_Subscription_Expiration
112
+ && $ emails ['WCS_Email_Customer_Notification_Free_Trial_Expiration ' ] instanceof WCS_Email_Customer_Notification_Free_Trial_Expiration
113
+ )
114
+ ) {
115
+ return ;
116
+ }
117
+ $ notification = null ;
118
+ switch ( current_action () ) {
119
+ case 'woocommerce_scheduled_subscription_customer_notification_auto_renewal ' :
120
+ $ notification = $ emails ['WCS_Email_Customer_Notification_Auto_Renewal ' ];
121
+ break ;
122
+ case 'woocommerce_scheduled_subscription_customer_notification_manual_renewal ' :
123
+ $ notification = $ emails ['WCS_Email_Customer_Notification_Manual_Renewal ' ];
124
+ break ;
125
+ case 'woocommerce_scheduled_subscription_customer_notification_trial_expiration ' :
126
+ $ notification = $ emails ['WCS_Email_Customer_Notification_Free_Trial_Expiration ' ];
127
+ break ;
128
+ case 'woocommerce_scheduled_subscription_customer_notification_expiration ' :
129
+ $ notification = $ emails ['WCS_Email_Customer_Notification_Subscription_Expiration ' ];
130
+ break ;
131
+ }
132
+
133
+ if ( $ notification ) {
134
+ $ notification ->trigger ( $ subscription_id );
135
+ }
136
+ }
137
+
32
138
/**
33
139
* Should the emails be sent out?
34
140
*
35
- * @return bool
141
+ * @return string yes|no
36
142
*/
37
143
public static function should_send_notification () {
144
+ $ notification_enabled = 'yes ' ;
145
+
38
146
if ( WCS_Staging::is_duplicate_site () ) {
39
- return false ;
147
+ $ notification_enabled = ' no ' ;
40
148
}
41
149
42
150
$ allowed_env_types = array (
43
151
'production ' ,
44
152
);
45
153
if ( ! in_array ( wp_get_environment_type (), $ allowed_env_types , true ) ) {
46
- return false ;
154
+ $ notification_enabled = 'no ' ;
155
+ }
156
+
157
+ /**
158
+ * Enables/disables all customer subscription notifications.
159
+ *
160
+ * Values 'yes' or 'no' expected, since it works with WC_Settings_API.
161
+ *
162
+ * @since 8.0.0
163
+ *
164
+ * @param string $notification_enabled
165
+ */
166
+ return apply_filters ( 'wcs_customer_email_notifications_enabled ' , $ notification_enabled );
167
+ }
168
+
169
+ /**
170
+ * Adds actions to the admin edit subscriptions page, if the subscription hasn't ended and the payment method supports them.
171
+ *
172
+ * @param array $actions An array of available actions
173
+ * @return array An array of updated actions
174
+ * @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.0
175
+ */
176
+ public static function add_notification_actions ( $ actions ) {
177
+ global $ theorder ;
178
+
179
+ if ( wcs_is_subscription ( $ theorder ) ) {
180
+ //TODO maybe send only for active, on hold subscriptions?
181
+ //
182
+ $ actions ['wcs_customer_notification_free_trial_expiration ' ] = esc_html__ ( 'Send Free Trial Expiration notification ' , 'woocommerce-subscriptions ' );
183
+ $ actions ['wcs_customer_notification_subscription_expiration ' ] = esc_html__ ( 'Send Subscription Expiration notification ' , 'woocommerce-subscriptions ' );
184
+ $ actions ['wcs_customer_notification_manual_renewal ' ] = esc_html__ ( 'Send Manual Renewal notification ' , 'woocommerce-subscriptions ' );
185
+ $ actions ['wcs_customer_notification_auto_renewal ' ] = esc_html__ ( 'Send Automatic Renewal notification ' , 'woocommerce-subscriptions ' );
47
186
}
48
187
49
- return true ;
188
+ return $ actions ;
50
189
}
51
190
}
0 commit comments