-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrz_shipped_email.php
211 lines (181 loc) · 6.75 KB
/
rz_shipped_email.php
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WC_Email' ) ) {
return;
}
/**
* Class RZ_Shipped_Email
*/
class RZ_Shipped_Email extends WC_Email {
/**
* Set email defaults
*/
public function __construct() {
// error_log('__construct() called!');
// Unique ID for custom email
$this->id = 'rz_shipped_email';
// Is a customer email
$this->customer_email = true;
// Title field in WooCommerce Email settings
$this->title = __( 'Shipped order', 'wc-simple-shipment-tracking' );
// Description field in WooCommerce email settings
$this->description = __( 'Shipped email is sent when an order processed for the customer who placed the order.', 'wc-simple-shipment-tracking' );
// Default heading and subject lines in WooCommerce email settings
$this->subject = apply_filters( 'rz_shipped_email_default_subject', __( 'Your order has been shipped', 'wc-simple-shipment-tracking' ) );
$this->heading = apply_filters( 'rz_shipped_email_default_heading', __( 'Your Order Has Been Shipped', 'wc-simple-shipment-tracking' ) );
// Email template file path
$this->template_html = 'emails/rz-customer-shipped-order.php';
$this->template_plain = 'emails/plain/rz-customer-shipped-order.php';
// Check if email template file exists in theme folder, if not, use plugin default template file
$wc_template_base_in_theme = get_template_directory() . '/woocommerce/';
if( file_exists( $wc_template_base_in_theme . $this->template_html ) ) {
$this->template_base = $wc_template_base_in_theme;
} else {
$this->template_base = plugin_dir_path( __FILE__ ) . 'templates/';
}
// Trigger email when order status changed to shipped
// Action to which we hook onto to send the email.
// add_action( 'woocommerce_order_status_changed', array( $this, 'myfunc_status_custom_notification' ), 10, 4 );
// Call parent constructor to load any other defaults not explicity defined here
parent::__construct();
}
// public function myfunc_register_email_action( $email_actions ) {
// $email_actions[] = 'woocommerce_order_status_wc-shipped';
// return $email_actions;
// }
public function myfunc_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
if( $order->has_status('shipped') ) {
// Getting all WC_emails objects
// $email_notifications = WC()->mailer()->get_emails();
// Sending the customized email
// $email_notifications['wc-shipped']->trigger( $order_id );
$this->trigger( $order_id );
}
}
/**
* Add shipped custom status to email actions list.
*/
// public function myfunc_register_email_action( $email_actions ) {
// $email_actions[] = 'woocommerce_order_status_wc-shipped';
// return $email_actions;
// }
/**
* Prepares email content and triggers the email
*
* @param int $order_id
*/
public function trigger( $order_id, $resend = false ) {
// Bail if no order ID is present
if ( ! $order_id )
return;
// Send shipped email only once and not on every order status change
if ( get_post_meta( $order_id, RZ_META_KEY_EMAIL_SENT, true ) && !$resend ) return;
// setup order object
$this->object = new WC_Order( $order_id );
// get order items as array
$order_items = $this->object->get_items();
//* Maybe include an additional check to make sure that the online training program account was created
/* Uncomment and add your own conditional check
$online_training_account_created = get_post_meta( $this->object->id, '_crwc_user_account_created', 1 );
if ( ! empty( $online_training_account_created ) && false === $online_training_account_created ) {
return;
}
*/
/* Proceed with sending email */
$this->recipient = $this->object->billing_email;
// replace variables in the subject/headings
$this->find[] = '{order_date}';
$this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );
$this->find[] = '{order_number}';
$this->replace[] = $this->object->get_order_number();
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
return;
}
// All well, send the email
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
// add order note about sending the email
$note_message = __( ($resend ? '"%s" email resent again to the customer.' : '"%s" email sent to the customer.'), 'wc-simple-shipment-tracking' );
$this->object->add_order_note( sprintf( $note_message, $this->title ) );
// Set order meta to indicate that the welcome email was sent
update_post_meta( $this->object->id, RZ_META_KEY_EMAIL_SENT, 1 );
}
/**
* get_content_html function.
*
* @return string
*/
public function get_content_html() {
return wc_get_template_html(
$this->template_html,
array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this
),
'',
$this->template_base
);
}
/**
* get_content_plain function.
*
* @return string
*/
public function get_content_plain() {
return wc_get_template_html(
$this->template_plain,
array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => false,
'plain_text' => true,
'email' => $this
),
'',
$this->template_base
);
}
/**
* Initialize settings form fields
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => 'Enable this email notification',
'default' => 'yes'
),
'subject' => array(
'title' => __( 'Subject', 'woocommerce' ),
'type' => 'text',
'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ),
'placeholder' => '',
'default' => ''
),
'heading' => array(
'title' => __( 'Email Heading', 'woocommerce' ),
'type' => 'text',
'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ),
'placeholder' => '',
'default' => ''
),
'email_type' => array(
'title' => __( 'Email type', 'woocommerce' ),
'type' => 'select',
'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
'default' => 'html',
'class' => 'email_type wc-enhanced-select',
'options' => array(
'plain' => __( 'Plain text', 'woocommerce' ),
'html' => __( 'HTML', 'woocommerce' ),
'multipart' => __( 'Multipart', 'woocommerce' ),
)
)
);
}
}