forked from MarieComet/wcs-sell-coupons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoo-sell-coupons.php
executable file
·681 lines (609 loc) · 29.1 KB
/
woo-sell-coupons.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<?php
/*
* Plugin Name: WooCommerce Sell Coupons
* Text Domain: wcs-sell-coupons
* Domain Path: /languages
* WC requires at least: 3.0.0
* WC tested up to: 3.6.2
* Plugin URI: https://github.com/MarieComet/wcs-sell-coupons
* Description: This plugin create a new WooCommerce product type and add possibilty to sell Coupons as Gift Card in front-office. Please visit WooCommerce > Settings > General once activated !
* Version: 1.0.2
* Author: Marie Comet
* Author URI: https://www.mariecomet.fr/
* License: GPLv2 or later
* @package WooCommerce Sell Coupons
*/
/**
* Register the custom product type after init
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
*
* Check if WooCommerce is active
*
**/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// define plugin url
define( 'WSC__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
// require register coupon product type class
require_once( WSC__PLUGIN_DIR . 'wcs-register-coupon.php' );
/**
* Class Woo_Sell_Coupons
*/
class Woo_Sell_Coupons {
private static $instance;
private $pluginlocale;
/**
* Register function.
*/
public static function register() {
if ( null === self::$instance ) {
self::$instance = new Woo_Sell_Coupons();
}
}
/**
* Load textdomain
*/
public static function load_textdomain() {
load_plugin_textdomain( 'wcs-sell-coupons', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
*
*/
public function wcs_admin_notices() {
if ( '' !== get_option( 'wcs_gift_coupon_prefix' ) ) {
return;
}
?>
<div class="notice notice-success is-dismissible">
<p>
<a href="<?php echo( admin_url( 'admin.php?page=wc-settings' ) ) ?>">
<?php _e( 'Please visit WooCommerce > Settings > General to setup WooCommerce Sell Coupons !', 'wcs-sell-coupons' ); ?>
</a>
</p>
</div>
<?php
}
/**
* Woo_Sell_Coupons constructor.
*/
public function __construct() {
add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) );
add_action( 'admin_notices', array($this, 'wcs_admin_notices'));
add_action( 'wp_enqueue_scripts', array( $this, 'wcs_register_plugin_styles' ) );
// Add custom option field in woocommerce general setting
add_filter( 'woocommerce_general_settings', array( $this, 'wc_coupon_setting_page') );
add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'wcs_email_friend'), 10);
add_action( 'woocommerce_add_to_cart_validation', array( $this, 'wcs_check_custom_fields'), 10, 5 );
add_action( 'wp_ajax_custom_data', array( $this, 'wcs_custom_data_callback') );
add_action( 'wp_ajax_nopriv_custom_data', array( $this, 'wcs_custom_data_callback') );
add_filter( 'woocommerce_add_cart_item_data', array( $this, 'wcs_add_cart_item_custom_data'), 10, 2 );
add_filter( 'woocommerce_get_cart_item_from_session', array( $this, 'wcs_get_cart_items_from_session'), 1, 3 );
add_filter( 'woocommerce_cart_item_name', array( $this, 'wcs_add_user_custom_session' ), 1, 3 );
add_filter( 'woocommerce_order_item_name', array( $this, 'wcs_woocommerce_order_custom_session' ), 10, 3 );
add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'wcs_add_values_to_order_item_meta' ), 10, 4 );
add_filter( 'woocommerce_order_item_display_meta_key', array( $this, 'wcs_add_order_formatted_key' ), 10, 2 );
add_action( 'woocommerce_before_cart_item_quantity_zero',array( $this, 'wcs_remove_user_custom_data_options_from_cart' ) ,1,1);
add_action( 'woocommerce_order_status_completed', array( $this, 'wcs_create_coupon_on_order_complete' ) );
//locale management for translations
add_action ( 'switch_locale', array( $this, 'wcs_switch_locale') , 10, 1 );
add_filter( 'plugin_locale', array( $this, 'wcs_correct_locale') , 100, 2 );
}
/**
* Function used to check if a product is a gift_coupon type.
*
* @param int $product_id The product ID to check
*
* @return boolean
*/
function check_if_coupon_gift($product_id) {
$terms = get_the_terms($product_id, 'product_type');
if($terms) {
foreach ( $terms as $term ) {
if($term->slug === 'gift_coupon') {
return true;
}
}
}
return false;
}
/**
* Register and enqueue style sheet.
*/
public function wcs_register_plugin_styles() {
/*
wp_register_style( 'wcs-sell-coupons', plugins_url( 'wcs-sell-coupons/wcs-sell-coupons.css', dirname(__FILE__) ) );
wp_enqueue_style( 'wcs-sell-coupons' );
*
*/
}
/**
*
* @param $settings
*
* Add custom option field in woocommerce general setting
*
* Hooked on woocommerce_general_settings
*
* @return array
*/
function wc_coupon_setting_page( $settings ) {
$settings[] = array(
'name' => __( 'Gift cards', 'wcs-sell-coupons' ),
'type' => 'title',
'desc' => '',
'id' => 'wcs_gift_coupon',
);
$settings[] = array(
'title' => __( 'Duration of gift cars', 'wcs-sell-coupons' ),
'desc' => '',
'id' => 'wcs_gift_coupon_duration',
'desc_tip' => __( 'Validity period of gift cards (in days)', 'wcs-sell-coupons' ),
'type' => 'number',
'default' => '',
'css' => 'min-width:300px;',
'placeholder' => __( '30', 'wcs-sell-coupons' ),
);
$settings[] = array(
'title' => __( 'Gift cards code prefix', 'wcs-sell-coupons' ),
'desc' => '',
'id' => 'wcs_gift_coupon_prefix',
'desc_tip' => __( 'Prefix used for coupon codes', 'wcs-sell-coupons' ),
'type' => 'text',
'default' => '',
'css' => 'min-width:300px;',
'placeholder' => __( 'GF', 'wcs-sell-coupons' ),
);
$settings[] = array(
'type' => 'sectionend',
'id' => 'wcs_gift_coupon'
);
return $settings;
}
/**
* Add two custom fields on the single product page before add to cart button.
*
* Hooked on woocommerce_before_add_to_cart_button
*/
function wcs_email_friend() {
global $woocommerce, $post;
if ( $this->check_if_coupon_gift( $post->ID ) ) {
echo '<div class="wcs-data">';
echo '<label for="wcs_email_friend">' . __('Recipient e-mail', 'wcs-sell-coupons') . ': <abbr class="required" title="'. __( 'required', 'wcs-sell-coupons' ).'">*</abbr></label>';
echo '<input type="email" id="wcs_email_friend" name="wcs_email_friend" placeholder="[email protected]" />';
echo '<label for="wcs_name_friend">' . __('Name of the recipient', 'wcs-sell-coupons') . ': <abbr class="required" title="'. __( 'required', 'wcs-sell-coupons' ).'">*</abbr></label>';
echo '<input type="text" id="wcs_name_friend" name="wcs_name_friend" placeholder="'. __( 'John Doe', 'wcs-sell-coupons' ).'"/>';
echo '<label for="wcs_gift_message">' . __('Gift message', 'wcs-sell-coupons') . ': </label>';
$gift_message = __( 'Sending you this gift coupon with best wishes', 'wcs-sell-coupons' );
$thumbnail = wp_get_attachment_image( get_post_thumbnail_id(), 'thumbnail' );
if ( $thumbnail ) { $gift_message .= '<br />' . $thumbnail; }
$gift_message = apply_filters('wcs_gift_message', $gift_message);
$gift_message_input = '<textarea id="wcs_gift_message" name="wcs_gift_message" placeholder="'. __( 'Add your gift message here.', 'wcs-sell-coupons' ).'"></textarea>';
// use add_filter('wcs_gift_message_input', 'custom_input', 10, 2); to override input type
$gift_message_input = apply_filters( 'wcs_gift_message_input', $gift_message_input, $gift_message );
echo $gift_message_input;
echo '</div>';
}
}
/**
* Our callback function receive our two custom fields value
* We create a new session with session_start(); and create two new session values : wcs_email_friend and wcs_name_friend
*
* Hooked on wp_ajax_nopriv_custom_data and wp_ajax_custom_data See ajax in WordPress
*/
function wcs_custom_data_callback() {
// We'll use this to post back the data the server received
print_r($_POST);
WC()->session->set('wcs_email_friend', $_POST['wcs_email_friend']);
WC()->session->set('wcs_name_friend', $_POST['wcs_email_friend']);
WC()->session->set('wcs_gift_message', $_POST['wcs_email_friend']);
// RIP
die();
}
/**
* Add a validation when a gift_coupon is added to cart
* Check if our two custom fields are not empty, if there are display a WC notice error
*
* Hooked on woocommerce_add_to_cart_validation
*
* @param $passed
* @param $product_id
* @param $quantity
*
* @return bool
*/
function wcs_check_custom_fields( $passed, $product_id, $quantity ) {
if ( $this->check_if_coupon_gift( $product_id ) ) {
if ( ! empty( $_POST['wcs_email_friend'] ) && ! empty( $_POST['wcs_name_friend'] ) ) {
$passed = true;
} else {
wc_add_notice( __( 'Please fill in the required fields', 'wcs-sell-coupons' ), 'error' );
$passed = false;
}
} else {
$passed = true;
}
return $passed;
}
/**
* Add our two custom field values storent in SESSION to the cart item object
*
* Hooked on woocommerce_add_cart_item_data
*
* @param $cart_item_meta
* @param $product_id
*
* @return mixed
*/
function wcs_add_cart_item_custom_data( $cart_item_meta, $product_id ) {
global $woocommerce;
if ( $this->check_if_coupon_gift( $product_id ) && ! empty( $_POST['wcs_email_friend'] ) && ! empty( $_POST['wcs_name_friend'] ) ) {
$cart_item_meta['wcs_email_friend'] = sanitize_email( $_POST['wcs_email_friend'] );
$cart_item_meta['wcs_name_friend'] = sanitize_text_field( $_POST['wcs_name_friend'] );
$cart_item_meta['wcs_gift_message'] = wp_kses_post( $_POST['wcs_gift_message'] );
}
return $cart_item_meta;
}
/**
* Get our custom field values in cart from session
*
* Hooked on woocommerce_get_cart_item_from_session
*
* @param $item
* @param $values
* @param $key
*
* @return mixed
*/
function wcs_get_cart_items_from_session( $item, $values, $key ) {
if ( array_key_exists( 'wcs_email_friend', $values ) ) {
$item['wcs_email_friend'] = $values['wcs_email_friend'];
}
if ( array_key_exists( 'wcs_name_friend', $values ) ) {
$item['wcs_name_friend'] = $values['wcs_name_friend'];
}
if ( array_key_exists( 'wcs_gift_message', $values ) ) {
$item['wcs_gift_message'] = $values['wcs_gift_message'];
}
return $item;
}
/**
* Display the custom field values next to the Product name in the cart
*
* Hooked on woocommerce_cart_item_name
*
* @param $product_name
* @param $values
* @param $cart_item_key
*
* @return string
*/
function wcs_add_user_custom_session( $product_name, $values, $cart_item_key ) {
if ( $this->check_if_coupon_gift($values['product_id'] ) && isset( $values['wcs_name_friend'] ) && isset( $values['wcs_email_friend'] ) ) {
$return_string = $product_name . "</br><span>" . __( 'To', 'wcs-sell-coupons' ) . ": " . $values['wcs_name_friend'] . " (" . $values['wcs_email_friend'] . ')</span>';
if ( isset( $values['wcs_gift_message'] ) && $values['wcs_gift_message'] ) {
$return_string .= "<br /><span>" . stripslashes($values['wcs_gift_message']) . '</span>';
}
return $return_string;
} else {
return $product_name;
}
}
/**
* Display custom field values in the order confirmation page
*
* Hooked on woocommerce_order_item_name
*
* @param $name
* @param $item
*
* @return string
*/
function wcs_woocommerce_order_custom_session( $name, $item ) {
if ( $this->check_if_coupon_gift( $item['product_id'] ) && isset( $item['name_to'] ) && isset( $item['mail_to'] ) ) {
$return_string = $name . "<br />" . $item['name_to'] . "<br />" . $item['mail_to'];
if ( isset( $item['gift_message'] ) && $item['gift_message'] ) {
$return_string .= "<br /><span>" . stripslashes($item['gift_message']) . '</span>';
}
return $return_string;
} else {
return $name;
}
}
/**
* Add our custom fields values as order item values, it can be seen in the order admin page and we can get it later
*
* Hooked on woocommerce_add_order_item_meta
*
* @param $item
* @param $cart_item_key
* @param $values
* @param $order
*
*/
function wcs_add_values_to_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( $this->check_if_coupon_gift( $values['product_id'] ) && isset( $values['wcs_name_friend'] ) && isset( $values['wcs_email_friend'] ) ) {
// lets add the meta data to the order
$item->update_meta_data( '_name_to', $values['wcs_name_friend'] );
$item->update_meta_data( '_mail_to', $values['wcs_email_friend'] );
$item->update_meta_data( '_gift_message', $values['wcs_gift_message'] );
}
}
/**
* Add $display_key for display it in admin
*
* Hooked on woocommerce_order_item_display_meta_key
*
* @param $display_key
* @param $meta
*
* @return string
*/
function wcs_add_order_formatted_key( $display_key, $meta ) {
if ( $meta->key === '_name_to' ) {
$display_key = __( 'Recipient', 'wcs-sell-coupons' );
}
if ( $meta->key === '_mail_to' ) {
$display_key = __( 'E-mail', 'wcs-sell-coupons' );
}
if ( $meta->key === '_gift_message' ) {
$display_key = __( 'Message', 'wcs-sell-coupons' );
}
if ( $meta->key === '_gift_code' ) {
$display_key = __( 'Coupon', 'wcs-sell-coupons' );
}
return $display_key;
}
/**
* Remove the cart content custom values when a product is deleted
*
* Hooked on woocommerce_before_cart_item_quantity_zero
*
* @param $cart_item_key
*/
function wcs_remove_user_custom_data_options_from_cart( $cart_item_key ) {
global $woocommerce;
// Get cart
$cart = $woocommerce->cart->get_cart();
// For each item in cart, if item is upsell of deleted product, delete it
foreach( $cart as $key => $values ) {
if ( $values['name_to'] === $cart_item_key || $values['mail_to'] === $cart_item_key || $values['gift_message'] == $cart_item_key )
unset( $woocommerce->cart->cart_contents[ $key ] );
}
}
/**
* @param $order_id
*
* @throws Exception
*
*
* Finally when an order is completed we create a coupon with the previously custom values
* This function is hooked on woocommerce_order_status_completed, when a payment is OK and NOT before.
* TODO: Orders currently go to Processing on payment, manual move to completion before sending coupon
* is unnecessary: for example if other goods are ordered at the same time
* order will not be completed until it is confirmed they have arrived
*/
function wcs_create_coupon_on_order_complete( $order_id ) {
// Instancy a new WC_Order class.
$order = new WC_Order( $order_id );
// Get each product in order.
$order_items = $order->get_items();
$duration_gift_coupon = get_option( 'wcs_gift_coupon_duration' );
$prefix_gift_coupon = get_option( 'wcs_gift_coupon_prefix' );
$today = time();
if ( ! empty( $duration_gift_coupon ) ) {
$date_expire = strtotime( "+".$duration_gift_coupon." days", $today );
} else { // default 30 days
$date_expire = strtotime( "+30 days", $today );
}
$expiry_date = wc_format_datetime( $date_expire );
foreach( $order_items as $order_product_detail => $values ) {
// check if the product is a gift_coupon !
if ( $this->check_if_coupon_gift( $values['product_id'] ) ) {
// Get the customer order values
$client_first_name = get_post_meta( $order_id, '_billing_first_name', true );
$client_last_name = get_post_meta( $order_id, '_billing_last_name', true );
// Create a nice name...
$client_name = $client_first_name .' '. $client_last_name;
// Get the product price (gift amount)
$product = wc_get_product( $values['product_id'] );
$product_price = $product->get_regular_price( 'edit' );
// Amount coupon code
$amount = $product_price; // Amount
// Get the custom order values : friend and email
$friend_email = $values['item_meta']['_mail_to'];
$friend_name = $values['item_meta']['_name_to'];
$friend_message = isset( $values['item_meta']['_gift_message'] ) ? stripslashes( $values['item_meta']['_gift_message'] ) : '';
//if coupon already issued, get it, don't issue again by changing the status repeatedly
$coupon_code = wc_get_order_item_meta( $order_product_detail, '_gift_code' );
$new_coupon_id = ( $coupon_code ) ? post_exists( $coupon_code ) : 0;
if ( ! $new_coupon_id ){
// Generate a random code
$coupon_code = strtolower( $prefix_gift_coupon.'_'.$this->wsc_random_number() );
// Construct our coupon post
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_excerpt' => __( 'To:', 'wcs-sell-coupons' ) . ' ' . $friend_name . ' ' . __( '- Sent to:', 'wcs-sell-coupons' ) . ' ' . $friend_email . ' ' . $friend_message,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon',
);
$new_coupon_id = wp_insert_post( $coupon );
}
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
/**
* Filters the coupon rules meta data to create.
*
* @param array $coupon_meta keyed array of coupon attributes.
*/
$coupon_meta = apply_filters('wcs_gift_coupon_meta', array(
'discount_type' => $discount_type ,
'coupon_amount' => $amount ,
'expiry_date' => $expiry_date,
'date_expires' => $date_expire,
'individual_use' => 'yes' ,
'usage_limit' => '1' ,
'usage_limit_per_user' => '1' ,
'limit_usage_to_x_items' => '1',
//record some information to help trace this later
'_name_to' => $friend_name,
'_mail_to' => $friend_email,
'_gift_message' => $friend_message,
'_gift_from' => $client_name,
'_gift_order' => $order_id
), $new_coupon_id, $coupon_code);
// Insert coupon meta
foreach ( $coupon_meta as $meta_key => $meta_value ) {
update_post_meta( $new_coupon_id, $meta_key, $meta_value );
}
//attach the coupon reference to the order after to the coupon is correctly created
//$item_id, $meta_key, $meta_value, $unique
wc_add_order_item_meta( $order_product_detail, '_gift_code', $coupon_code, TRUE );
// Finally send an email to the receiver with the coupon ID, client name, receiver email and name
$this->wcs_sendEmail($order, $new_coupon_id, $client_name, $friend_email, $friend_name, $friend_message, $coupon_code);
}
}
}
/**
* @param $order WC_Order
* @param $post
* @param $client_name
* @param $email
* @param $name
* @param $friend_message
* @param $coupon_code
*
* TODO: $post is currently post of type coupon but may need order for language and copy original customer..
* TODO: better to do all from order, then can implement resend on order screen
* TODO: use WC_Coupon class rather than post for coupon data
*
*/
public function wcs_sendEmail ( $order, $post, $client_name, $email, $name, $friend_message, $coupon_code ) {
if ( function_exists( 'pll_get_post_language' ) ) {
$locale = pll_get_post_language( $order->get_id(), 'locale' );
switch_to_locale( $locale );
}
// Get the coupon code amount
$coupon_amount = get_post_meta($post, 'coupon_amount', true);
$coupon_expire = get_post_meta($post, 'date_expires', true);
$coupon_has_expired = false;
if ( $coupon_expire && strtotime( $coupon_expire ) && ( current_time( 'timestamp', true ) > strtotime( $coupon_expire ) ) ) {
$coupon_has_expired = true;
}
$usage_count = get_post_meta($post, 'usage_count', true);
//coupons are in shop base currency not current user/order currency so get unfiltered base ccy
$formatted_price = wc_price($coupon_amount, array('currency' => get_option( 'woocommerce_currency' ), ));
/**
* allow theme to apply special formatting including link to auto-add-coupon to basket
* ( ?apply_coupon=coupon_code requires plugin, not implemented in woocommerce core)
* could also add fancy formatting / additional message and QR codes
*
* @param string $formatted_coupon_code default formatting
* @param string $coupon_code raw coupon code
* @param string $coupon_amount raw coupon amount
* @param string $formatted_price formatted coupon amount
*/
$formatted_coupon_code = apply_filters('wcs_format_gift_coupon',
'<h2>' . $coupon_code . '</h2>',
$coupon_code, $coupon_amount, $formatted_price);
// Construct email datas
//get_current_site()->site_name; //wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$blogname = get_bloginfo( 'name', 'display' );
$blogurl = get_home_url();
$shopurl = wc_get_page_permalink ('shop');
$subject = '[' . $blogname . '] ' . $client_name . ' ' . __(' offers you a gift card !', 'wcs-sell-coupons' ) ;
$sendEmail = get_bloginfo( 'admin_email' );
$headers = array('Content-Type: text/html; charset=UTF-8');
// Instancy a new WC Mail class
$mailer = WC()->mailer();
ob_start();
echo '<style >';
wc_get_template( 'emails/email-styles.php' );
echo '</style>';
$messageStyle = ob_get_clean();
$email_heading = __( 'Your gift card to use on ', 'wcs-sell-coupons' ) . ' <a href="' . $blogurl .'">'. $blogname . '</a>';
$toEmail = $email;
$theMessage = $friend_message . ' <h2>' . __('Hello ', 'wcs-sell-coupons' ) . ' ' . $name . ',</h2><p>'. $client_name . ' ' .
__(' offers you a gift card ', 'wcs-sell-coupons' ) . ' ' . $formatted_price . ' ' .
__(' to use on ', 'wcs-sell-coupons') . ' <a href="' . $shopurl .'">'. $blogname . '</a>.</p><br />';
if ( $usage_count || $coupon_has_expired ){
$theMessage .= '<h2>' . $coupon_code . '</h2> ';
} else {
$theMessage .= '<p>' . __("To use it enter this code : ", 'wcs-sell-coupons' ) .
' <strong>' . $coupon_code . '</strong> ' .
__( 'in your basket during your purchase.', 'wcs-sell-coupons') . '</p>';
$theMessage .= $formatted_coupon_code;
}
if ( $coupon_expire ) {
$formatted_coupon_expire = date( "Y-m-d", $coupon_expire );
if ( $coupon_has_expired ){
$theMessage .= '<p>' . sprintf( __( 'Please note: this coupon expired on %s and cannot be used, this email is for information only.', 'wcs-sell-coupons' ), $formatted_coupon_expire ) . '</p>';
} else {
$theMessage .= '<p>' . __( "Warning, this coupon is only valid until ", 'wcs-sell-coupons' ) . ' ' . $formatted_coupon_expire . ' !</p>';
}
}
if ( $usage_count ) {
$theMessage .= '<p>' . __( 'Please note: this coupon is already used and cannot be used again, this email is for information only.', 'wcs-sell-coupons' ) . '</p>';
}
$theMessage .= '<h3>' . __( 'See you soon on ','wcs-sell-coupons' ) . ' <a href="' . $blogurl .'">'. $blogname . '</a> !</h3>';
$messageBody = $mailer->wrap_message( $email_heading, $theMessage );
$attachment = '';
// Send the email
$mailer->send( $toEmail, $subject, $messageStyle . $messageBody, $headers, $attachment );
//message to forward
$forwardedMessage = wptexturize( '<br />-------------' . __( 'Copy of Message', 'wcs-sell-coupons' ) . '-------------<br />' .
__( 'To:', 'wcs-sell-coupons' ) . ' ' . $name . ' <' . $toEmail . '><br />' .
__( 'Subject:', 'wcs-sell-coupons' ) . ' ' . $subject . '<br /><br />')
. $messageBody;
// Send copy of email to client
$custEmail = $order->get_billing_email();
$custSubject = __( 'Your gift coupon was sent to:','wcs-sell-coupons' ) . ' ' . $name;
$mailer->send( $custEmail, $custSubject, $messageStyle . $custSubject . $forwardedMessage, $headers, $attachment );
// Send copy of email to shop admin
//move to class implementation as $this->get_option( 'recipient', get_option( 'admin_email' ) );
$shopEmail = get_option( 'admin_email' );
$shopSubject = __( 'Gift coupon was issued for order:','wcs-sell-coupons' ) . ' ' . $order->get_id();
$mailer->send( $shopEmail, $shopSubject, $messageStyle . $shopSubject . $forwardedMessage, $headers, $attachment );
}
/**
*
* Function to generate custom number used by wcs_create_coupon_on_order_complete and use wp_generate_password function.
*
* @return string
*/
function wsc_random_number() {
$random_number = wp_generate_password( 15, false );
return $random_number;
}
/**
* Fires when the locale is switched.
*
* @since 4.7.0
*
* @param string $locale The new locale.
*/
public function wcs_switch_locale( $locale ){
$this->pluginlocale = $locale;
$this->load_textdomain();
}
/**
* @param $locale
* @param $domain
*
*
*
* @return mixed
*/
public function wcs_correct_locale( $locale, $domain ){
if ( $this->pluginlocale ){
return $this->pluginlocale;
} else {
return $locale;
}
}
}
Woo_Sell_Coupons::register();
}