Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit ab9085e

Browse files
committed
Added order notes and actions.
1 parent 8f8cda8 commit ab9085e

10 files changed

+359
-392
lines changed

includes/class-wc-subscriptions-email-notifications.php

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,71 @@ class WC_Subscriptions_Email_Notifications {
1313
public static function init() {
1414

1515
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+
);
1681
}
1782

1883
/**
@@ -29,23 +94,97 @@ public static function add_emails( $email_classes ) {
2994
return $email_classes;
3095
}
3196

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+
32138
/**
33139
* Should the emails be sent out?
34140
*
35-
* @return bool
141+
* @return string yes|no
36142
*/
37143
public static function should_send_notification() {
144+
$notification_enabled = 'yes';
145+
38146
if ( WCS_Staging::is_duplicate_site() ) {
39-
return false;
147+
$notification_enabled = 'no';
40148
}
41149

42150
$allowed_env_types = array(
43151
'production',
44152
);
45153
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' );
47186
}
48187

49-
return true;
188+
return $actions;
50189
}
51190
}

includes/emails/class-wcs-email-customer-notification-auto-renewal.php

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
* @package WooCommerce_Subscriptions/Classes/Emails
1414
* @extends WC_Email
1515
*/
16-
class WCS_Email_Customer_Notification_Auto_Renewal extends WC_Email {
16+
class WCS_Email_Customer_Notification_Auto_Renewal extends WCS_Email_Customer_Notification {
1717

1818
/**
1919
* Create an instance of the class.
2020
*/
2121
public function __construct() {
22+
$this->plugin_id = 'woocommerce-subscriptions_';
2223

2324
$this->id = 'customer_notification_auto_renewal';
2425
$this->title = __( 'Customer Notification: Automatic Renewal for Subscription', 'woocommerce-subscriptions' );
@@ -38,97 +39,5 @@ public function __construct() {
3839

3940
// TODO: check if this interferes with the UX via option setting.
4041
$this->enabled = WC_Subscriptions_Email_Notifications::should_send_notification();
41-
42-
add_action( 'woocommerce_scheduled_subscription_customer_notification_auto_renewal', array( $this, 'trigger' ) );
43-
}
44-
45-
/**
46-
* Get the default e-mail subject.
47-
*
48-
* @return string
49-
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.5.3
50-
*/
51-
public function get_default_subject() {
52-
return $this->subject;
53-
}
54-
55-
/**
56-
* Get the default e-mail heading.
57-
*
58-
* @return string
59-
* @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.5.3
60-
*/
61-
public function get_default_heading() {
62-
return $this->heading;
63-
}
64-
65-
/**
66-
* trigger function.
67-
*
68-
* @return void
69-
*/
70-
public function trigger( $subscription ) {
71-
$this->object = $subscription;
72-
$this->recipient = $subscription->get_billing_email();
73-
74-
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
75-
return;
76-
}
77-
78-
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
79-
}
80-
81-
/**
82-
* get_content_html function.
83-
*
84-
* @return string
85-
*/
86-
public function get_content_html() {
87-
return wc_get_template_html(
88-
$this->template_html,
89-
array(
90-
'subscription' => $this->object,
91-
'email_heading' => $this->get_heading(),
92-
'additional_content' => is_callable(
93-
array(
94-
$this,
95-
'get_additional_content',
96-
)
97-
) ? $this->get_additional_content() : '',
98-
// WC 3.7 introduced an additional content field for all emails.
99-
'sent_to_admin' => true,
100-
'plain_text' => false,
101-
'email' => $this,
102-
),
103-
'',
104-
$this->template_base
105-
);
106-
}
107-
108-
/**
109-
* get_content_plain function.
110-
*
111-
* @return string
112-
*/
113-
public function get_content_plain() {
114-
return wc_get_template_html(
115-
$this->template_plain,
116-
array(
117-
'subscription' => $this->object,
118-
'email_heading' => $this->get_heading(),
119-
'additional_content' => is_callable(
120-
array(
121-
$this,
122-
'get_additional_content',
123-
)
124-
) ? $this->get_additional_content() : '',
125-
// WC 3.7 introduced an additional content field for all emails.
126-
'sent_to_admin' => true,
127-
'plain_text' => true,
128-
'email' => $this,
129-
),
130-
'',
131-
$this->template_base
132-
);
13342
}
13443
}

0 commit comments

Comments
 (0)