PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.5.2
EmailKit – Email Customizer for WooCommerce & WP v1.5.2
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Emails / Woocommerce / CustomerNote.php

CustomerNote.php in EmailKit – Email Customizer for WooCommerce & WP 1.5.2, at includes/Admin/Emails/Woocommerce/CustomerNote.php

171 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\Emails\Woocommerce;
4
5 use WP_Query;
6 use EmailKit\Admin\Emails\EmailLists;
7
8 defined("ABSPATH") || exit;
9
10
11 class CustomerNote
12 {
13
14 private $db_query_class = null;
15
16 public function __construct()
17 {
18 $args = [
19 'post_type' => 'emailkit',
20 'meta_query' => [
21 [
22 'key' => 'emailkit_template_type',
23 'value' => EmailLists::CUSTOMER_NOTE,
24 ],
25 [
26 'key' => 'emailkit_template_status',
27 'value' => 'Active',
28 ],
29 ],
30 ];
31
32 $this->db_query_class = new WP_Query($args);
33
34 if (isset($this->db_query_class->posts[0])) {
35 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
36 }
37
38 add_filter('woocommerce_new_customer_note_notification', [$this, 'noteCustomer'], 10, 1);
39 }
40
41 /**
42 * @param $email_class
43 */
44 public function remove_woocommerce_emails($email_class)
45 {
46
47 remove_action('woocommerce_new_customer_note_notification', [$email_class->emails['WC_Email_Customer_Note'], 'trigger']);
48 }
49
50 /**
51 * @param $order
52 */
53 public function noteCustomer($order)
54 {
55
56
57 $query = $this->db_query_class;
58 $email = get_option('admin_email');
59
60 if (isset($query->posts[0])) {
61
62 if (!empty($order)) {
63 $defaults = [
64 'order_id' => '',
65 'customer_note' => ''
66 ];
67
68 $order = wp_parse_args($order, $defaults);
69
70 $order_id = $order['order_id'];
71 $customer_note = $order['customer_note'];
72 }
73 if ($order_id) {
74 $order = wc_get_order($order_id);
75 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
76
77 $woocommerce_currency_settings = get_woocommerce_currency_symbol();
78 $replacements = [];
79 foreach ($order->get_items() as $item_id => $item) {
80
81 $id = $item['product_id'];
82 $product_name = $item['name'];
83 $item_qty = $item['quantity'];
84 $item_total = $item['total'];
85 $product = wc_get_product($id);
86 $product_price = $product->get_price() * $item_qty;
87
88 // Use the currency symbol from the WooCommerce settings
89 $currency_symbol = $woocommerce_currency_settings;
90
91 // Format the product price with the currency symbol
92 $formatted_product_price = $currency_symbol . number_format($product_price, 2);
93
94 // Format the item total with the currency symbol
95 $formatted_item_total = $currency_symbol . number_format($item_total, 2);
96
97
98 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price];
99
100 }
101
102 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
103
104 $shipping_first_name = $order->get_shipping_first_name();
105 $shipping_last_name = $order->get_shipping_last_name();
106 $shipping_full_name = $shipping_first_name . ' ' . $shipping_last_name;
107
108 $details = [
109 "{{order_id}}" => $order_id,
110 "{{customer_note}}" => $customer_note,
111 "{{order_number}}" => $order->get_order_number(),
112 "{{order_status}}" => $order->get_status(),
113 "{{order_subtotal}}" => wc_price( $order->get_subtotal()),
114 "{{order_currency}}" => $order->get_currency(),
115 "{{billing_phone}}" => $order->get_billing_phone(),
116 "{{shipping_total}}" => wc_price( $order->get_shipping_total() ),
117 "{{shipping_tax_total}}" => wc_format_decimal($order->get_shipping_tax(), 2),
118 "{{order_date}}" => gmdate('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
119 "{{shipping_method}}" => $order->get_shipping_method(),
120 "{{payment_method}}" => $order->get_payment_method_title(),
121 "{{total}}" => wc_price($order->get_total(), 2),
122 "{{billing_name}}" => $order->get_formatted_billing_full_name(),
123 "{{billing_first_name}}" => $order->get_billing_first_name(),
124 "{{billing_last_name}}" => $order->get_billing_last_name(),
125 "{{billing_company}}" => $order->get_billing_company(),
126 "{{billing_address_1}}" => $order->get_billing_address_1(),
127 "{{billing_address_2}}" => $order->get_billing_address_2(),
128 "{{billing_city}}" => $order->get_billing_city(),
129 "{{billing_state}}" => $order->get_billing_state(),
130 "{{billing_postcode}}" => $order->get_billing_postcode(),
131 "{{billing_country}}" => $order->get_billing_country(),
132 "{{billing_email}}" => $order->get_billing_email(),
133 "{{shipping_name}}" => $shipping_full_name,
134 "{{shipping_first_name}}" => $shipping_first_name,
135 "{{shipping_last_name}}" => $shipping_last_name,
136 "{{shipping_company}}" => $order->get_shipping_company(),
137 "{{shipping_address_1}}" => $order->get_shipping_address_1(),
138 "{{shipping_address_2}}" => $order->get_shipping_address_2(),
139 "{{shipping_city}}" => $order->get_shipping_city(),
140 "{{shipping_state}}" => $order->get_shipping_state(),
141 "{{shipping_postcode}}" => $order->get_shipping_postcode(),
142 "{{shipping_country}}" => $order->get_shipping_country(),
143 "{{shipping_phone}}" => $order->get_shipping_phone(),
144 "{{download_permissions}}" => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
145 ];
146
147 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
148
149 $to = $order->get_billing_email();
150 $order_date = $order->get_date_created();
151 $formatted_date = esc_attr($order_date->date('F j, Y'));
152
153 $pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
154 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("customer notes added ", 'emailkit');
155 $subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true));
156 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : esc_html__("Note added to your order on ", 'emailkit') . $formatted_date ." " . esc_html__("on", "emailkit"). " " . esc_attr(get_bloginfo('name')). ' - ' . $pre_header;
157
158
159
160 $headers = [
161 'From: ' . $email . "\r\n",
162 'Reply-To: ' . $email . "\r\n",
163 'Content-Type: text/html; charset=UTF-8',
164 ];
165
166 wp_mail($to, $subject, $message, $headers);
167 }
168 }
169 }
170 }
171