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

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