-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjustificatif-etudiant.php
271 lines (225 loc) · 9.77 KB
/
justificatif-etudiant.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
include_once dirname(WC_PLUGIN_FILE) . '/includes/emails/class-wc-email.php';
if (!class_exists('WC_Email_Justificatif_Etudiant')):
class WC_Email_Justificatif_Etudiant extends WC_Email
{
public function __construct()
{
$this->id = 'justificatif_etudiant';
$this->title = __('Demande de justificatif étudiants', 'orgues-nouvelles');
$this->recipient = $this->get_option('recipient', get_option('admin_email', '[email protected]'));
$this->description = __('Cet email est envoyé lorsqu\'un abonné choisit le tarif étudiant pour lui demander d\'envoyer un justificatif.', 'orgues-nouvelles');
$this->template_html = 'emails/justificatif_etudiant.php';
$this->template_plain = 'emails/plain/justificatif_etudiant.php';
$this->template_base = plugin_dir_path(__FILE__) . 'templates/';
$this->placeholders = array(
'{order_date}' => '',
'{order_number}' => '',
);
add_action('woocommerce_checkout_order_created', array($this, 'trigger'));
// Call parent constructor
parent::__construct();
}
function trigger($order)
{
if (!$this->is_enabled() || !is_a($order, 'WC_Order')) {
return;
}
$this->setup_locale();
$items = $order->get_items();
$demande_justif = false;
foreach ($items as $item) {
$variation_id = $item->get_variation_id();
if ($variation_id) {
$demande = get_post_meta($variation_id, '_demande_justificatif', true); // Récupère la valeur de la métadonnée
if ($demande === "yes") { // Vérifie si la valeur est 'yes'
$demande_justif = true;
break;
}
}
}
if ($demande_justif) {
$this->object = $order;
$customer_email = $order->get_billing_email();
$this->recipient = $customer_email;
$this->placeholders['{order_date}'] = wc_format_datetime($this->object->get_date_created());
$this->placeholders['{order_number}'] = $this->object->get_order_number();
$this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
}
$this->restore_locale();
}
public function get_headers()
{
$headers = "Content-Type: " . $this->get_content_type() . "\r\n";
$headers .= "From: " . $this->get_admin_recipient() . "\r\n";
$headers .= "Reply-To: " . $this->get_admin_recipient() . "\r\n";
$headers .= "Cc: " . $this->get_admin_recipient() . "\r\n";
return apply_filters('woocommerce_email_headers', $headers, $this->id, $this->object);
}
public function get_admin_recipient()
{
return $this->get_option('admin_recipient', get_option('admin_email', ''));
}
public function get_default_subject()
{
return __('[{site_title}]: Demande de justificatif pour votre commande', 'orgues-nouvelles');
}
/**
* Get email heading.
*
* @since 3.1.0
* @return string
*/
public function get_default_heading()
{
return __('Demande de justificatif pour votre Abonnement', 'orgues-nouvelles');
}
public function init_form_fields() {
parent::init_form_fields();
$form_fields = $this->form_fields;
$this->form_fields = array();
foreach ($form_fields as $key => $value) {
if ($key === 'subject') {
$this->form_fields['admin_recipient'] = array(
'title' => __('Copy email', 'woocommerce'),
'type' => 'email',
'desc_tip' => true,
'description' => __('Email address to send a copy of this email to.', 'woocommerce'),
'placeholder' => $this->recipient,
'default' => '',
);
}
if ($key === 'additional_content') {
$this->form_fields['body'] = array(
'title' => __('Contenu de l\'email', 'orgues-nouvelles'),
'type' => 'textarea',
'css' => 'width:600px; height: 150px;',
'description' => __('Texte de l\'email envoyé aux clients lorsqu\'ils choisissent le tarif étudiant pour leur demander un justificatif.', 'orgues-nouvelles'),
'default' => $this->get_default_body(),
'desc_tip' => true,
);
}
$this->form_fields[$key] = $value;
}
$this->form_fields['body'] = array(
'title' => __('Contenu de l\'email', 'orgues-nouvelles'),
'type' => 'textarea',
'description' => __('Texte de l\'email envoyé aux clients lorsqu\'ils choisissent le tarif étudiant pour leur demander un justificatif.', 'orgues-nouvelles'),
'default' => $this->get_default_body(),
'desc_tip' => true,
);
}
public function get_default_body()
{
return __('Nous vous demandons de bien vouloir nous fournir un justificatif de votre statut étudiant pour valider votre abonnement.', 'orgues-nouvelles');
}
/**
* Returns the email body content.
*
* @since 1.7.0
*
* @return string HTML
*/
public function get_body()
{
$email_id = strtolower($this->id);
/**
* Filters the membership email body.
*
* @since 1.7.0
*
* @param string $body email body content
* @param \WC_Email_Justificatif_Etudiant current email instance
*/
$body = (string) apply_filters("{$email_id}_email_body", $this->format_string($this->get_option('admin_recipient')), $this->object);
if (empty($body) || !is_string($body) || '' === trim($body)) {
$body = $this->get_default_body();
}
// convert relative URLs to absolute for links href and images src attributes
$domain = get_home_url();
$replace = array();
$replace['/href="(?!https?:\/\/)(?!data:)(?!#)/'] = 'href="' . $domain;
$replace['/src="(?!https?:\/\/)(?!data:)(?!#)/'] = 'src="' . $domain;
$body = preg_replace(array_keys($replace), array_values($replace), $body);
return $body;
}
public function is_customer_email()
{
return true;
}
/**
* Returns the arguments that should be passed to an email template.
*
* @since 1.12.0
*
* @param array<string, mixed> $args default args
* @return array<string, mixed> associative array
*/
protected function get_template_args($args = []): array
{
return [
'order' => $this->object,
'email' => $this,
'email_heading' => $this->get_heading(),
'email_body' => $this->get_body(),
'additional_content' => $this->get_additional_content(),
];
}
/**
* Returns the email HTML content.
*
* @since 1.7.0
*
* @return string HTML
*/
public function get_content_html()
{
$args = array('plain_text' => false);
ob_start();
wc_get_template($this->template_html, array_merge($args, $this->get_template_args($args)),'', $this->template_base);
return ob_get_clean();
}
/**
* Returns the email plain text content.
*
* @since 1.7.0
*
* @return string plain text
*/
public function get_content_plain()
{
$args = array('plain_text' => true);
ob_start();
wc_get_template($this->template_html, array_merge($args, $this->get_template_args($args)),'', $this->template_base);
return ob_get_clean();
}
}
add_action('woocommerce_email_classes', 'add_justificatif_etudiant_email');
function add_justificatif_etudiant_email($email_classes)
{
$email_classes['WC_Email_Justificatif_Etudiant'] = new WC_Email_Justificatif_Etudiant();
return $email_classes;
}
endif;
// Ajoute le champ à l'interface d'administration des variations
add_action('woocommerce_product_after_variable_attributes', 'ajouter_champ_justificatif', 10, 3);
function ajouter_champ_justificatif($loop, $variation_data, $variation)
{
woocommerce_wp_checkbox(array(
'id' => 'demande_justificatif[' . $loop . ']',
'label' => __('Demande de justificatif', 'orgues-nouvelles'),
'description' => __('Cochez cette case pour demander un justificatif pour cette variation.', 'orgues-nouvelles'),
'desc_tip' => true,
'value' => get_post_meta($variation->ID, '_demande_justificatif', true),
));
}
// Enregistre la valeur du champ
add_action('woocommerce_save_product_variation', 'enregistrer_champ_justificatif', 10, 2);
function enregistrer_champ_justificatif($variation_id, $i)
{
if (isset($_POST['demande_justificatif'][$i])) {
update_post_meta($variation_id, '_demande_justificatif', 'yes');
} else {
update_post_meta($variation_id, '_demande_justificatif', 'no');
}
}