PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.2
EmailKit – Email Customizer for WooCommerce & WP v1.6.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.6.2, at includes/Admin/Emails/Woocommerce/CustomerNote.php

175 lines 7.0 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 use EmailKit\Admin\Emails\Helpers\Utils;
8
9 defined("ABSPATH") || exit;
10
11
12 class CustomerNote
13 {
14
15 private $db_query_class = null;
16
17 public function __construct()
18 {
19 $args = [
20 'post_type' => 'emailkit',
21 'meta_query' => [
22 [
23 'key' => 'emailkit_template_type',
24 'value' => EmailLists::CUSTOMER_NOTE,
25 ],
26 [
27 'key' => 'emailkit_template_status',
28 'value' => 'Active',
29 ],
30 ],
31 ];
32
33 $this->db_query_class = new WP_Query($args);
34
35 if (isset($this->db_query_class->posts[0])) {
36 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
37 }
38
39 add_filter('woocommerce_new_customer_note_notification', [$this, 'noteCustomer'], 10, 1);
40 }
41
42 /**
43 * @param $email_class
44 */
45 public function remove_woocommerce_emails($email_class)
46 {
47
48 remove_action('woocommerce_new_customer_note_notification', [$email_class->emails['WC_Email_Customer_Note'], 'trigger']);
49 }
50
51 /**
52 * @param $order
53 */
54 public function noteCustomer($order)
55 {
56
57
58 $query = $this->db_query_class;
59 $email = get_option('admin_email');
60
61 if (isset($query->posts[0])) {
62
63 if (!empty($order)) {
64 $defaults = [
65 'order_id' => '',
66 'customer_note' => ''
67 ];
68
69 $order = wp_parse_args($order, $defaults);
70
71 $order_id = $order['order_id'];
72 $customer_note = $order['customer_note'];
73 }
74 if ($order_id) {
75 $order = wc_get_order($order_id);
76 $html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true);
77
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
89 // Format the product price with the currency symbol
90 $formatted_product_price = wc_price($product_price);
91
92 // Format the item total with the currency symbol
93 $formatted_item_total = wc_price($item_total);
94
95 $product_image_id = $product->get_image_id();
96 $product_image_url = $product_image_id ? wp_get_attachment_url($product_image_id) : wc_placeholder_img_src();
97
98 $product_sku = $product->get_sku();
99 $attributes = [];
100 $meta_data = $item->get_meta_data();
101
102 foreach ($meta_data as $meta) {
103 // Check for product attribute (pa_ is the prefix for standard attributes)
104 if (strpos($meta->key, 'pa_') === 0) {
105 // Standard product attribute
106 $formatted_name = ucwords(wc_attribute_label(str_replace('pa_', '', $meta->key), $product));
107 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : ''; // Capitalize the value
108
109 // Add the attribute to the list with HTML markup
110 $attributes[] = esc_html($formatted_name) . ': ' . esc_html($formatted_value);
111
112 } else {
113
114 $formatted_name = is_string($meta->key) ? ucwords(str_replace('_', ' ', strtolower($meta->key))) : '';
115 $formatted_value = is_string($meta->value) ? ucwords(strtolower($meta->value)) : '';
116
117 // Add the custom meta to the list with HTML markup
118 $attributes[] = esc_html($formatted_name) . ':' . esc_html($formatted_value);
119
120 }
121 }
122
123 $formatted_attributes = !empty($attributes) ? implode(', ', $attributes) : '';
124
125
126 $replacements[] = [$product_name, $item_qty, $formatted_item_total, $formatted_product_price, $product_image_url, $product_sku, $formatted_attributes];
127
128 }
129
130 $html = \EmailKit\Admin\Emails\Helpers\Utils::order_items_replace($html, $replacements);
131
132 // Order details array for email
133 $details = Utils::woocommerce_order_email_contents($order);
134
135 $details["{{customer_note}}"] = $customer_note;
136
137 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
138
139 $currency_symbol_position = get_option('woocommerce_currency_pos');
140
141 if($currency_symbol_position == 'left') {
142
143 $message = Utils::adjust_price_structure($message);
144
145 } else if($currency_symbol_position == 'left_space') {
146
147 $message = Utils::adjust_left_space_price_structure($message);
148
149 }
150
151 $to = $order->get_billing_email();
152 $order_date = $order->get_date_created();
153 $formatted_date = esc_attr($order_date->date('F j, Y'));
154
155 $pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
156 $pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template);
157 $pre_header = !empty($pre_header) ? $pre_header : esc_html__("customer notes added ", 'emailkit');
158 $subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true);
159 $subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template);
160 $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;
161
162
163
164 $headers = [
165 'From: ' . $email . "\r\n",
166 'Reply-To: ' . $email . "\r\n",
167 'Content-Type: text/html; charset=UTF-8',
168 ];
169
170 wp_mail($to, $subject, $message, $headers);
171 }
172 }
173 }
174 }
175